Garbage collecting locals

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Garbage collecting locals

Falko Poiker
Hello,

Do local variables get garbage collected when the block that contains them
is over?  If not, is there a way to force this? 
Do locals get garbage collected first during the garbage collection phase?
Is there a way to force garbage collection in general?

We have a problem where many new instances of an object are repeatedly
created (as local variables), but are not being destroyed....  It's too bad
the lua book doesn't cover garbage collection yet...   It's a very hazy
topic for me.

Falko

Reply | Threaded
Open this post in threaded view
|

Re: Garbage collecting locals

Luiz Henrique de Figueiredo
>Do local variables get garbage collected when the block that contains them
>is over?

Just to be picky: the *values* of the local variables for which there are no
other references are collected in the next cycle after the block ends.

>Do locals get garbage collected first during the garbage collection phase?

No order is guaranteed.

>Is there a way to force garbage collection in general?

collectgarbage()
--lhf

Reply | Threaded
Open this post in threaded view
|

RE: Garbage collecting locals

Falko Poiker
In reply to this post by Falko Poiker
> >Do local variables get garbage collected when the 
> >block that contains them is over?
> Just to be picky: the *values* of the local variables 
> for which there are no other references are collected 
> in the next cycle after the block ends.
How do you define "for which there are no other references"?

To wit:
I create a local variable that is a userdata pointer to an object in my c++
code.  At the end of the block, how does lua know (or what determines) if
there are no other references to that object?   

Basically, I was hoping that the fact that the userdata is local indicates
that the variable value would be collected once it's out of scope (so that
the object the userdata points to gets deleted as well).  

> >Is there a way to force garbage collection in general?
> collectgarbage()
I find it strange that there is no C API function that does the same thing,
only a lua library function.  What if the code lua is embedded in wants to
force garbage collection?

Thanks!
Falko

Reply | Threaded
Open this post in threaded view
|

RE: Garbage collecting locals

Luiz Henrique de Figueiredo
In reply to this post by Falko Poiker
>How do you define "for which there are no other references"?

Lua's GC is mark-and-sweep: in each cycle, any value that is the value of
a global variable, table field, upvalue, stack value (including locals),
and perhaps some others that I forget right now, is marked. Every value that is
not marked is freed (or perhaps it's the other way around).

>I create a local variable that is a userdata pointer to an object in my c++
>code.  At the end of the block, how does lua know (or what determines) if
>there are no other references to that object?   

Lua doesn't know. It infers so, but only during the next GC cycle. The end of
a block does not trigger GC.

>Basically, I was hoping that the fact that the userdata is local indicates
>that the variable value would be collected once it's out of scope (so that
>the object the userdata points to gets deleted as well).  

Like I said, it does so, but only when GC is run.

>> collectgarbage()
>I find it strange that there is no C API function that does the same thing,
>only a lua library function.  What if the code lua is embedded in wants to
>force garbage collection?

Of course there is a C API function for that: it is used to implement
"collectgarbage":

static int luaB_collectgarbage (lua_State *L) {
  lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
  return 0;
}

So, to force GC at any time in C, call lua_setgcthreshold(L,0);

--lhf