lua-users home
lua-l archive

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


On Tue, Mar 31, 2015 at 11:58 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> Is an internalized string marked for garbage collection
> when there are no live references to it?

Hello, Dirk

thank you for this question. I had no idea that Lua interns short string.

I think, I found the answer in source of function internshrstr, which
creates a short string [1].

This function calls function createstrobj, the same function which is
called by luaS_newlstr (general string creator). Function createstrobj
uses GCObject.

So, the answer is "yes, internalized string is marked for garbage
collection when there are no live references to it".

I've created the test to confirm that short Lua strings are collected:

list = {}
for i = 1, 10000000 do
    local str = 's' .. math.random(0, 1000000000)
    table.insert(list, str)
    if math.random(0, 100) == 0 then
        list = {} -- without this line, it consumes 1.3G
    end
end

This script consumes almost no memory. I think, I can conclude that
short strings are collected.

[1] http://www.lua.org/source/5.3/lstring.c.html#internshrstr

Best regards,
Boris Nagaev