lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Jun 26, 2014, at 12:50 AM, Thomas Jericke <tjericke@indel.ch> wrote:

> On 06/26/2014 07:26 AM, Paige DePol wrote:
>> On Jun 25, 2014, at 11:26 PM, Daurnimator <quae@daurnimator.com> wrote:
>> 
>>> Today I was reading the lua source, and came across this code in lmem.c ( http://www.lua.org/source/5.2/lmem.c.html#luaM_realloc_ )
>>> 
>>>     if (g->gcrunning) {
>>>       luaC_fullgc(L, 1);  /* try to free some memory... */
>>> 
>>> Is that conditional accidentally reversed?
>>> Why run the gc again ONLY if the gc is mid-run?
>>> Wouldn't it make sense to run the gc if it's NOT already running?
>>> 
>>> Sorry if this is a stupid question >.<
>>> 
>>> Daurn.
>> If the realloc routine fails to allocate memory, and the garbage collector is running, then it will attempt to do a full garbage collection cycle (in emergency mode) in the effort to free memory.
>> 
>> The allocation is then tried again, and if it fails a memory error is thrown. If the garbage collector is not active then this emergency collection will not occur.
>> 
>> Note that the `g->gcrunning` flag only indicates if the garbage collector is running, not if it is in the middle of a run or any other status other than active or inactive.
>> 
>> ~pmd
> 
> Now that explains why the emergency gc didn't run when I ran out of memory. I have disabled the GC and run it in steps when my system is idle.
> 
> Now I am wondering if there is an option to turn the GC of but still enable the emergency GC.
> --
> Thomas

In vanilla Lua, it does not appear so, you would have to modify the `luaM_realloc_` function to add such functionality yourself. I also run the GC in steps for my game engine so this is something I need to consider as well.

I suppose simply removing the check if the GC is running and always forcing a full GC check on allocation failure could work, but I do not know if there would be any side effects to that. However, a cursory scan for `gcrunning` seems to indicate that it is not used by the actual GC itself, it is just a flag to indicate if the GC is activated.

~pmd