Clean lua stack

Started by
8 comments, last by JTippetts 11 years, 11 months ago
Hello,
I'm trying to clean the lua stack by using the following piece of code:

int i=0, prev=1,count=0;
while(i!=prev || count<10){
lua_settop (luaVM, 0);
lua_gc (luaVM, LUA_GCCOLLECT, 0);
prev=i;
i=lua_gc(luaVM, LUA_GCCOUNT, 0);
count++;
}

But it doesn't clean entirely. I still have variables that are not 'nil' when the while loop has finished. The variable 'i' never turn zero but I guess that is expected. I do a minimum of 10 garbage collections just to be on the safe side.

Thanks for reading.
Advertisement
Just being curious. Why do you not delete and recreate the vm ?
If you are determining that the stack is not empty via the return value of lua_gc then you are mistaken. The stack is not the only place when memory is used and a separate stack space is given to each C function which Lua calls, for instance there is _G the global table or _ENV the environment table in 5.2 (of which there can be many) and also the registry. Added to this a variable which is on the stack and also stored in another place will not be collected.

Rest assured your stack is empty and after such a time that there are no more references to the data, without you doing a gc (never mind 10), the memory will be released.
Thanks for the response. Yeah, I could re-initialize lua by closing, opening it and registering my functions. I thought my lua variables would be nil when running a script again after gc had been done... well I guess that's not the case. Any ideas how this can be accomplished? i.e.

if initial==nil then initial=0 end

So that it holds true every time I run the script after a gc or similar.
In your code, it is pointless to call lua_gc() with LUA_GCCOLLECT 10 times, since LUA_GCCOLLECT forces a full garbage collection cycle. Any garbage that can be collected, will be collected. Subsequent calls will have no garbage to collect, so they just waste time. If you want to do incremental garbage collection, use LUA_GCSTEP and use the last parameter to control the step size. Tweak the value until you get something you like; too low and your garbage pile may outpace your collector, too high might adversely affect your simulation loop if you are generating lots of garbage.

Garbage collection will only clean up "garbage"; that is, variables and objects that are no longer actively referenced in your Lua state. If you have a global variable, i, that is still referenced in the global table of your state, then it won't be collected. The state is persistent between running scripts.

One way of ensuring that variables do not stay persistent between calls to dofile() or similar is to use the local keyword. Any variable declared local within a script chunk will be garbage collected after the script chunk is finished (unless a reference to the variable is returned and stored elsewhere non-locally). Conversely, you could cleanup before your script exits by explicitly setting any objects or variables to nil.
Thanks for the reply. What I would like is a method to clean the stack of variables and functions that are declared from a script file. Right now the easiest way seems to be lua_close followed by lua_open and re-register my C fuctions.
Well, like I said, if you declare those variables and functions local inside the script, then they will be automatically cleared when the script exits. Say you have a Lua file that consists of this:


-- test1.lua
a=3


From a Lua prompt, execute call dofile("test1.lua"). After that executes, call print(a) and the result will be 3.

Then, modify the script file to this:


-- test2.lua
local a=3


From a new prompt, use dofile to execute that file then again call print(a). The result this time now will be nil, since the variable a is local to the script.

If you want a bit more control over the permanence of some things, you can use tables to your advantage. In the script, declare all functions and variables and assign them to be members of a table, rather than lurking in the global space:


-- test3.lua
localspace=
{
a=3,
f=function(g) print(g) end
}


After that script executes, the only thing in global state will be the localspace table. Then, you can easily wipe everything the script declared simply by setting localspace to nil; everything it contains will be sent to the garbage heap.

In fact, many references will recommend that you use levels of nesting, rather than dumping everything in the global environment directly. With enormous programs that have lots of data, it can make a difference, since Lua data structures reduce down to a lot of map-type data structures, and searching a set of nested structures can require overall fewer operations than searching one big global map.
Well, I want the variable to be persistant from frame to frame (script is called each frame), but nil the first frame. The script is associated with an episode, so each time I start the episode the value should be nil. I use lua_pcall if I remember correctly.. I'm at my day job right now.. Sorry if it sounds fuzzy. :-)
Back at home I tried using a table and it works excellent. I'm not very experienced with tables yet but I've found them quite useful =)
Tables are the very heart and soul of Lua. Lua wouldn't be 1/10th as useful without them. :D

Glad you got it figured out.

This topic is closed to new replies.

Advertisement