lua-users home
lua-l archive

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


> lua-l,
> 
> I have been looking at how to manage/minimize memory usage in a C/Lua process/program running on Linux. I have observed some non-deterministic behavior with Lua’s collectgarbage() in “freeing” up memory for other processes to be able to use.
> 
> Details may be found here:
> https://stackoverflow.com/questions/65375638/in-lua-is-there-a-limitation-to-the-number-of-objects-tables-that-can-be-determ
> 
> Specifically, is there a way to ensure all unused memory is freed from within Lua?

Calling 'collectgarbage' twice frees all memory not in use by Lua.
"Frees" here means to call 'free'; that does not ensure the memory is
returned to the OS and therefore is available to other processes.
That is all that can be done in ISO C.

Returning that free memory to the OS is a task to the memory allocator.
It probably depends on the memory fragmentation, specifically whether
the free memory forms blocks large enough to be returned. And
fragmentation depends on the pattern of memory allocations and, again,
on how the allocator behaves. All of this seems to be outside the
control of Lua.

-- Roberto