lua-users home
lua-l archive

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


On 2020/11/28 2:44, André Henriques wrote:
> Hi everyone!
> 
> I am working on a code that calls a Lua function with an object argument with the C API.
> 
> When the Lua function body is empty, the program consumes a lot of memory:
> 
> function callback(object) end
> 
> However, if I add a variable declaration, the memory usage is low throughout the execution:
> 
> function callback(object)
>     local _ = {}
> end
> 
> I would like to know if this is expected and why.
> 
> I made a test program to measure memory consumption depending on some parameters:
> 
> usage: ./test dummy_table_bool test_iterations userdata_size
> 
> Results:
> 
> ...
>     if (withDummyTable) {
>         luaL_dostring(luaState, "function callback(object) local _ = {} end");
>     } else {
>         luaL_dostring(luaState, "function callback(object) end");
>     }
> ...
> 
> André Henriques
> 

Just a wild guess, when you create a dummy table, this triggers a gc cycle, while the empty
callback doesn't allocate anything thus gc didn't have a chance to run.

try the following to verify whether this is the case, this code force a full gc:


```
void test(lua_State *luaState, unsigned testIterationsCount, size_t userDataSize) {
    for (unsigned i = 0; i < testIterationsCount; ++i) {
        lua_getglobal(luaState, "callback");
        pushObject(luaState, userDataSize);
        lua_call(luaState, 1, 0);
    }
    lua_gc(LUA_GCCOLLECT);
}
```

or alternatively, you may run a single gc step in every iteration to amortize the gc overhead.