dreamworkAPI
Ctrl K
GitHub
libraryDocs / gc

gc

librarysharedmenusource

Lua manages memory automatically by running a garbage collector to collect all dead objects (that is, objects that are no longer accessible from Lua).

All memory used by Lua is subject to automatic management: strings, tables, userdata, functions, threads, internal structures, etc.

Functions

gc.collectPerforms a full garbage-collection cycle.
gc.getMemoryThe value has a fractional part, so that it multiplied by 1024 gives the exact number of bytes in use by Lua (except fo…
gc.getTableRulesReturns info about the garbage collector rules for the given table.
gc.isRunningReturns a boolean that tells whether the collector is running (i.e., not stopped).
gc.restartRestarts automatic execution of the garbage collector.
gc.setPauseThe garbage-collector pause controls how long the collector waits before starting a new cycle.
gc.setStepThe garbage-collector step multiplier controls the relative speed of the collector relative to memory allocation.
gc.setStepMultiplierIf you set the step multiplier to a very large number (larger than 10% of the maximum number of bytes that the program…
gc.setTableRulesSets the garbage-collector rules for the given table.
gc.setupRegisters the table for automatic cleanup: when an object of the given type is destroyed, its key is removed from the t…
gc.stopStops automatic execution of the garbage collector.
gc.unsetupStops the automatic cleanup of the table for the given type.

#gc.collect

sharedmenusource
function gc.collect()

Performs a full garbage-collection cycle.

#gc.getMemory

sharedmenunodiscardsource
function gc.getMemory(): number

The value has a fractional part, so that it multiplied by 1024 gives the exact number of bytes in use by Lua (except for overflows).

Returns
TypeNameDescription
numbercountThe total memory in use by Lua in Kbytes.

#gc.getTableRules

sharedmenunodiscardsource
function gc.getTableRules(tbl: table): boolean, boolean

Returns info about the garbage collector rules for the given table.

Parameters
NameTypeDescription
tbltableThe table to query.
Returns
TypeNameDescription
booleankey_modetrue if key c ollection is allowed, false otherwise.
booleanvalue_modetrue if value collection is allowed, false otherwise.

#gc.isRunning

sharedmenunodiscardsource
function gc.isRunning(): boolean

Returns a boolean that tells whether the collector is running (i.e., not stopped).

Returns
TypeNameDescription
booleanis_runningReturns true if the collector is running, false otherwise.

#gc.restart

sharedmenusource
function gc.restart()

Restarts automatic execution of the garbage collector.

#gc.setPause

sharedmenusource
function gc.setPause(value: number): number

The garbage-collector pause controls how long the collector waits before starting a new cycle. Larger values make the collector less aggressive.

Values smaller than 100 mean the collector will not wait to start a new cycle. A value of 200 means that the collector waits for the total memory in use to double before starting a new cycle.

Parameters
NameTypeDescription
valuenumberThe new value for the pause of the collector.
Returns
TypeNameDescription
numberpause_valueThe previous value for pause.

#gc.setStep

sharedmenusource
function gc.setStep(size: number): boolean

The garbage-collector step multiplier controls the relative speed of the collector relative to memory allocation. Larger values make the collector more aggressive but also increase the size of each incremental step.

You should not use values smaller than 100, because they make the collector too slow and can result in the collector never finishing a cycle. The default is 200, which means that the collector runs at "twice" the speed of memory allocation.

Parameters
NameTypeDescription
sizenumberWith a zero value, the collector will perform one basic (indivisible) step. For non-zero values, the collector will perform as if that amount of memory (in KBytes) had been allocated by Lua.
Returns
TypeNameDescription
booleanfinishedReturns true if the step finished a collection cycle.

#gc.setStepMultiplier

sharedmenusource
function gc.setStepMultiplier(value: number): number

If you set the step multiplier to a very large number (larger than 10% of the maximum number of bytes that the program may use), the collector behaves like a stop-the-world collector. If you then set the pause to 200, the collector behaves as in old Lua versions, doing a complete collection every time Lua doubles its memory usage.

Parameters
NameTypeDescription
valuenumberThe new value for the step multiplier of the collector.
Returns
TypeNameDescription
numberprevious_valueThe previous value for step.

#gc.setTableRules

sharedmenusource
function gc.setTableRules(tbl: table, key?: boolean, value?: boolean)

Sets the garbage-collector rules for the given table.

Parameters
NameTypeDescription
tbltableThe table to set the rules for.
keyoptionalbooleantrue if key collection is allowed, false otherwise.
valueoptionalbooleantrue if value collection is allowed, false otherwise.

#gc.setup

sharedsource
function gc.setup(tbl: table, type_name: gc.Type, instruction?: gc.Instruction)

Registers the table for automatic cleanup: when an object of the given type is destroyed, its key is removed from the table with raw.set, bypassing __newindex.

Calling it again with the same table and type replaces the instruction, one table can be registered for several types.

NOTE: The registry does not keep the table alive, but LuaJIT weak tables are not ephemerons, so an instruction that captures tbl as an upvalue keeps it alive forever.

NOTE: The instruction is called during the entity removal and must not call gc.setup or raise errors, an error skips the cleanup of the remaining tables.

Parameters
NameTypeDescription
tbltableThe table to clean up.
type_namegc.TypeThe type of objects used as keys in the table.
instructionoptionalgc.InstructionThe function that maps the destroyed object to the key, by default the object itself is the key.

#gc.stop

sharedmenusource
function gc.stop()

Stops automatic execution of the garbage collector. The collector will run only when explicitly invoked, until a call to restart it.

#gc.unsetup

sharedsource
function gc.unsetup(tbl: table, type_name: gc.Type)

Stops the automatic cleanup of the table for the given type.

Parameters
NameTypeDescription
tbltableThe table to stop cleaning up.
type_namegc.TypeThe type the table was registered with.
Generated from the dreamwork meta branch.