Lua memory leak in C process -
i have c program running lua.
although try use lua_gc() , control memory usage of lua, c process memory usage still high. c process uses on 150mb memory though lua uses 4mb memory.
i try use l_alloc() function track lua memory allocation, result same memory usage lua told calling lua_gc(lua_gccount) , lua_gc(lua_gccountb).
after calling lua_close() close lua environment, process memory down , looks fine. therefore, think 'lost memory' still controlled lua not c program.
here sample c code. creates lua environment, calls lua function purge data , check memory usage.
int main() { int rc; uint64_t gc_mem_usage; lua_state* lua = lual_newstate(); lual_openlibs(lua); lua_gc(lua, lua_gcstop, 0); lual_dofile(lua, "test.lua"); gc_mem_usage = ((uint64_t)lua_gc(lua, lua_gccount, 0) << 10) + lua_gc(lua, lua_gccountb, 0); printf("lua mem usage: [%" priu64 "] bytes\n", gc_mem_usage); lua_getglobal(lua, "command_handler"); lua_pushstring(lua, "cc"); rc = lua_pcall(lua, 1, 0, 0); if (rc != 0 ) { printf("function error\n"); return; } lua_settop(lua, 0); // full gc lua_gc(lua, lua_gccollect, 0); lua_gc(lua, lua_gccollect, 0); // don't know why has different result calling full gc twice sleep(1); printf("-------------after gc ----------------------\n"); gc_mem_usage = ((uint64_t)lua_gc(lua, lua_gccount, 0) << 10) + lua_gc(lua, lua_gccountb, 0); printf("lua mem usage: [%" priu64 "] bytes\n", gc_mem_usage); // infinite-loop while(1); }
lua sample code:
local abc = {} function command_handler(cmd) if (cmd == "cc") abc = {} end end =0, 2000000 abc[i] = "abc" .. .. "def" end
output:
lua mem usage: [204913817] bytes -------------after gc ---------------------- lua mem usage: [4219342] bytes
the output told me lua memory usage down after gc, memory usage of c process still high (193.7mb) checking atop continuously.
pid minflt majflt vstext vsize rsize vgrow rgrow mem cmd 1/1 4622 1 0 3k 193.7m 183.9m 0k 4k 18% a.out
is there solution reduce c process memory usage?
my environment lua 5.1.4 running in ubuntu/centos.
lua faithfully frees unreachable objects calling supplied deallocation function (by default realloc(block, 0)
). looks libc
allocator struggling return unused memory, possibly due high fragmentation. looking @ strace
output (i've got same numbers lua 5.1.4 on 64-bit debian 6), c runtime chooses allocate using brk
small increments, no deallocation (calling brk
lower value) follows. however, if insert malloc_trim(m_top_pad)
before entering infinite loop, you'll see in top
output resident size drops drastically ~5m , strace
reveals data segment indeed trimmed brk
. using custom allocator (e.g. pool-based) or tuning malloc
parameters in situation.
Comments
Post a Comment