lua-users home
lua-l archive

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


Hello,

I have some questions about Lua's full GC behavior at memory allocation failure.

In LuaFaq, I found the following description:

http://www.luafaq.org/#T1.9
8.2.11 Emergency garbage collection
A full garbage collection is forced when allocation fails. This is particularly useful in embedded devices where memory is a constrained resource.

Is this really true?
In our embedded project, we found that this might not be always true, which means an allocation failure cannot trigger full GC in some cases
where we know we could actually release enough space if we could trigger full GC at that timing.

This actually prevents our system from working, where the amount memory(RAM) is very limited, say, to around 100KB.
Since this RAM is shared between Lua and other components on the system, delay of Lua's full GC can cause other parts to fail to allocate
dynamic memory.

To workaround this issue, we have added the following code into lmem.c:luaM_realloc(), to forcefully trigger full GC when frealloc() fails:
 ...
 block = (*g->frealloc(...);
+if(block == NULL && nsize > 0){
+    lua_gc(L,LUA_GCCOLLECT,100);
+    block = (*g->frealloc(...);
+}

Is this an appropriate approach for this purpose? Or, is there any other good way to achieve the same result?

At least, we have found a problem in the above-mentioned code:
This is because during GC triggered via lua_gc() luaM_realloc() is called again in lgc.c: checkSizes() - luaZ_resizebuffer() path, which leads to
a kind of recursive call. So, this workaround is not acceptable to us.

Instead of adding workaround into Lua itself like above, should I somehow solve the issue in application side, for example, catching LUA_ERRMEM
with pcall() and calling lua_gc() there?

What I would like to achieve is to trigger and actually run full GC anytime we want to, especially at a memory allocation failure.
What is the best approach to this issue? Any advice would be helpful.

Thanks,
Daisuke