Skip to content

Commit 0bc2e6f

Browse files
committed
Update guided tour
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
1 parent 92633a6 commit 0bc2e6f

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ These tools make it easy to compile, analyze, and embed `Luau` scripts directly
2525
- Bidirectional [function calls](#function-calls) between Zig and Lua
2626
- First-class [userdata support](#userdata-integration) including metamethods
2727
- Native support for refs, functions, tables, and vector types
28+
- [Fine-grained garbage collection control](#garbage-collection) for memory management
2829
- Supports Luau code generation for improved performance on supported platforms
2930
- Built-in [Luau tools](#-using-luau-tools) (`luau-compile` and `luau-analyze`) provided by the build system
3031
- Excellent [test coverage](https://app.codecov.io/gh/mxpv/luaz) and API [documentation](https://mxpv.github.io/luaz/#luaz.lua)
@@ -210,6 +211,7 @@ pub fn main() !void {
210211
}
211212
```
212213

214+
213215
> [!WARNING]
214216
> This library is still evolving and the API is not stable. Backward incompatible changes may be introduced up until the 1.0 release. Consider pinning to a specific commit or tag if you need stability.
215217
@@ -220,7 +222,6 @@ The following features are planned after the initial release:
220222
- Luau sandbox APIs - Safe execution environments for untrusted code
221223
- Coroutine / threads support - Full Luau coroutine and threading capabilities
222224
- Debug APIs - Debugging hooks and introspection tools
223-
- GC API - Fine-grained garbage collection control
224225
- Optional JSON support - Built-in JSON serialization/deserialization
225226

226227
## 🛠️ Using Luau Tools

examples/guided_tour.zig

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! - Use Luau's codegen feature
1313
//! - Push and retrieve arrays
1414
//! - Work with tuples and table-based structures
15+
//! - Control garbage collection for memory management
1516

1617
const std = @import("std");
1718
const luaz = @import("luaz");
@@ -472,6 +473,82 @@ pub fn main() !void {
472473
print("Retrieved point coordinates: x={d:.1}, y={d:.1}\n", .{ x_coord.?, y_coord.? });
473474
}
474475

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+
475552
// Error handling
476553
{
477554
print("\n-- Error Handling --\n", .{});

0 commit comments

Comments
 (0)