Reputation: 4109
I have a number of Lua scripts running in mac Mac OS application along with Objective C code. The memory footprint of my application continues to rise over time. Therefore, I decided to call collectgarbage
function periodically from my Lua script. Since, I am new to Lua, I am not sure if I should call it in every script or calling it from any of the script is fine i.e. does it collect the garbage from all open Lua states or just from the states associated with the current Lua file?
Upvotes: 2
Views: 5903
Reputation: 13869
If you have access to all of your Lua states, you can use the lua_close
function which collects all garbage from the Lua state that you pass to it as an argument.
void lua_close (lua_State *L);
This, however, frees all objects in the Lua state.
The other more reasonable alternative from the C API is the lua_gc
method.
int lua_gc (lua_State *L, int what);
Either way you'll need to pass it a state, and the garbage collector will only clear the dynamic memory used by that specific state.
For more information, look up lua_close
and lua_gc
in the manual:
http://www.lua.org/manual/5.2/manual.html
Upvotes: 0
Reputation: 473537
Memory, like everything in Lua, is per-Lua state. Different Lua states are completely separate, and there is no (direct) way for anything done in one Lua state to affect the contents of another.
Also, Lua doesn't really know what a "script" is. Or a file. There's just the code the interpreter happens to be currently executing. It certainly does not keep track of memory on a per-"script" basis.
collectgarbage
therefore works on the level that it can: it collects garbage from the current Lua state.
Upvotes: 2