collectgarbage()

Type Function
Library (globals)
Return value none
Revision Release 2022.3683
Keywords garbage collection, garbage, memory, resources

Overview

A generic interface to Lua's garbage collector. Using this function is not required because Lua has automatic garbage collection features.

Syntax

collectgarbage( [opt [, arg]] )
opt (required)

String. Performs garbage collection functions differently based on this option. Acceptable values include:

  • "collect" — performs a full garbage-collection cycle. This is the default option.
  • "stop" — stops the garbage collector.
  • "restart" — restarts the garbage collector.
  • "count" — returns the total memory in use by Lua (in kilobytes).
  • "step" — performs a garbage-collection step. The step "size" is controlled by arg (larger values mean more steps) in a non-specified way. If you want to control the step size you must experimentally tune the value of arg. Returns true if the step finished a collection cycle.
  • "setpause" — sets arg as the new value for the pause of the collector. Returns the previous value for pause.
  • "setstepmul" — sets arg as the new value for the step multiplier of the collector. Returns the previous value for step.
arg (optional)

Any. Arguments that are directly related to the value you chose for opt. Many options do not need arguments to be passed.