Garbage Collection API

The GC singleton provides script-level control over the garbage collector for performance tuning and memory management.

Note: GC is automatically available as a global variable at VM startup. No import needed.

Pause & Resume

GC.pause()

Pauses automatic garbage collection. Objects continue to accumulate in memory.

Returns: the GC object (for chaining)

GC.resume()

Resumes automatic garbage collection after a pause.

Returns: the GC object (for chaining)

GC.isPaused()

Checks whether garbage collection is currently paused.

Returns: boolean

Collection

GC.cycle()

Forces an immediate garbage collection cycle. If GC is paused, it temporarily re-enables for this cycle and pauses again.

Returns: the GC object (for chaining)

// Force cleanup after a large temporary allocation
GC.cycle()

// Works even while paused:
GC.pause()
// ... allocate lots of temporary objects ...
GC.cycle()    // Collects garbage, then re-pauses

Memory Info

GC.getBytesTracked()

Returns the number of bytes currently allocated and tracked by the garbage collector.

GC.getBytesThreshold()

Returns the current GC threshold — the byte count at which the next automatic collection triggers.

Threshold Configuration

GC.setBytesThreshold(threshold)

Sets the GC threshold. Values below 1024 are clamped to 1024 to prevent thrashing.

Returns: the GC object (for chaining)

// Higher threshold = less frequent collections
GC.setBytesThreshold(1048576)   // 1 MB

// Lower threshold = more aggressive collection
GC.setBytesThreshold(65536)     // 64 KB

Common Patterns

Performance-Critical Section

batch allocation
GC.pause()

var i = 0
while (i < 100000) {
    // ... heavy allocation work ...
    i = i + 1
}

GC.cycle()     // Clean up all at once
GC.resume()    // Back to normal

Memory Monitoring

var before = GC.getBytesTracked()
doExpensiveWork()
var after = GC.getBytesTracked()
print("Allocated: " + str(after - before) + " bytes")

See also: Continuations APIList APIMap API