|
12 | 12 | //! - Use Luau's codegen feature |
13 | 13 | //! - Push and retrieve arrays |
14 | 14 | //! - Work with tuples and table-based structures |
| 15 | +//! - Control garbage collection for memory management |
15 | 16 |
|
16 | 17 | const std = @import("std"); |
17 | 18 | const luaz = @import("luaz"); |
@@ -472,6 +473,82 @@ pub fn main() !void { |
472 | 473 | print("Retrieved point coordinates: x={d:.1}, y={d:.1}\n", .{ x_coord.?, y_coord.? }); |
473 | 474 | } |
474 | 475 |
|
| 476 | + // Garbage collection control |
| 477 | + { |
| 478 | + print("\n-- Garbage Collection --\n", .{}); |
| 479 | + |
| 480 | + const gc = lua.gc(); |
| 481 | + |
| 482 | + // Monitor memory usage |
| 483 | + const initial_memory_kb = gc.count(); |
| 484 | + const initial_memory_bytes = gc.countBytes(); |
| 485 | + const initial_total = initial_memory_kb * 1024 + initial_memory_bytes; |
| 486 | + print("Initial memory usage: {} bytes ({} KB + {} bytes)\n", .{ initial_total, initial_memory_kb, initial_memory_bytes }); |
| 487 | + |
| 488 | + // Create some objects to increase memory usage |
| 489 | + try lua.eval( |
| 490 | + \\local large_table = {} |
| 491 | + \\for i = 1, 1000 do |
| 492 | + \\ large_table[i] = "String number " .. i .. " with some extra data to use more memory" |
| 493 | + \\end |
| 494 | + \\global_table = large_table -- Keep reference to prevent immediate collection |
| 495 | + , .{}, void); |
| 496 | + |
| 497 | + const after_alloc_memory = gc.count(); |
| 498 | + print("Memory after allocation: {} KB (increased by {} KB)\n", .{ after_alloc_memory, after_alloc_memory - initial_memory_kb }); |
| 499 | + |
| 500 | + // Force garbage collection |
| 501 | + print("Running full garbage collection...\n", .{}); |
| 502 | + gc.collect(); |
| 503 | + const after_gc_memory = gc.count(); |
| 504 | + print("Memory after GC: {} KB\n", .{after_gc_memory}); |
| 505 | + |
| 506 | + // Fine-tune GC parameters |
| 507 | + print("Adjusting GC parameters...\n", .{}); |
| 508 | + const old_goal = gc.setGoal(150); // Start GC at 50% memory increase |
| 509 | + const old_stepmul = gc.setStepMul(300); // More aggressive GC |
| 510 | + const old_stepsize = gc.setStepSize(2048); // Larger GC steps |
| 511 | + print("Previous GC settings - Goal: {}, StepMul: {}, StepSize: {}\n", .{ old_goal, old_stepmul, old_stepsize }); |
| 512 | + |
| 513 | + // Demonstrate manual GC control |
| 514 | + print("Stopping GC for manual control...\n", .{}); |
| 515 | + gc.stop(); |
| 516 | + print("GC running: {}\n", .{gc.isRunning()}); |
| 517 | + |
| 518 | + // Create more garbage while GC is stopped |
| 519 | + try lua.eval("global_table = nil", .{}, void); // Release reference |
| 520 | + try lua.eval( |
| 521 | + \\for i = 1, 100 do |
| 522 | + \\ local temp = {} |
| 523 | + \\ for j = 1, 50 do |
| 524 | + \\ temp[j] = "Temporary string " .. i .. "," .. j |
| 525 | + \\ end |
| 526 | + \\end |
| 527 | + , .{}, void); |
| 528 | + |
| 529 | + // Perform stepped collection |
| 530 | + print("Performing manual GC steps...\n", .{}); |
| 531 | + var steps: u32 = 0; |
| 532 | + while (!gc.step(200) and steps < 5) { |
| 533 | + steps += 1; |
| 534 | + print("GC step {} completed\n", .{steps}); |
| 535 | + } |
| 536 | + print("GC cycle completed in {} steps\n", .{steps}); |
| 537 | + |
| 538 | + // Restart normal GC |
| 539 | + gc.restart(); |
| 540 | + print("GC restarted, running: {}\n", .{gc.isRunning()}); |
| 541 | + |
| 542 | + // Restore original GC parameters |
| 543 | + _ = gc.setGoal(old_goal); |
| 544 | + _ = gc.setStepMul(old_stepmul); |
| 545 | + _ = gc.setStepSize(old_stepsize); |
| 546 | + print("GC parameters restored\n", .{}); |
| 547 | + |
| 548 | + const final_memory = gc.count(); |
| 549 | + print("Final memory usage: {} KB\n", .{final_memory}); |
| 550 | + } |
| 551 | + |
475 | 552 | // Error handling |
476 | 553 | { |
477 | 554 | print("\n-- Error Handling --\n", .{}); |
|
0 commit comments