Re: Expectations on when GC is called

  • From: Mike Pall <mike-1403@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 6 Mar 2014 18:42:43 +0100

Saptarshi Guha wrote:
>    M = function()
>      myObj  = createRObject()
>      myObj = functionToProtect(myObj)
>      mObj = ffi.gc( myObject, functionToUnProtect)
>      -- do something
>    return nil

You're writing to global variables. That's not a good idea in
general.

>    So when the lua code returns to C(after the pcall), i call
> 
>    lua_gc(L, LUA_GCCOLLECT, 0);
> 
>    to trigger the GC. I expect the lua GC to run and myObj to be collected
> and
>    functionToUnProtect to be run.
> 
>    However nothing is printed.

Sure, the global keeps the object alive.

>  If i repeat steps (1) and (2) again, then it does get collected.

Because the global now holds the new object and the old object is
no longer anchored.

>  1. Doesn't lua_gc call the GC?

It does. But that's an expensive thing to do. Not something you'd
want to do after every call.

> 2. If so, do i have expectations on the time bounds or ordering of
> collections?

Nope.

--Mike

Other related posts: