lua-users home
lua-l archive

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


Kevin Baca escribió:

> I had a similar issue where I wanted to index tables using an opaque
> C-generated id object.

> The id was encapsulated in a user data.

> Each call to getId() generated a new user data, so it was not possible
> to use it as an index.  The solution was to convert the id to a unique
> string and use that as a key.

Wouldn't a better solution have been to generate the same userdata for
each id? Or alternatively, use a number or lightuserdata instead of
a character string?

> I discarded whole thing because of the number of strings that would be
> generated and because of my (possibly wrong) understanding that lua
> doesn't GC strings.

Lua garbage collects strings.

U:\>lua
Lua 5.0  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
> =gcinfo()
17      29
> a = {}
> for i = 1, 5000 do
>>   a[i] = string.rep("a", i)
>> end
> =gcinfo()
12474   13446
> a = nil
> =collectgarbage()
> =gcinfo()
34      68

-- However, there is some persistence

> =collectgarbage()
> =gcinfo()
25      50
> =collectgarbage()
> =gcinfo()
21      41
> =collectgarbage()
> =gcinfo()
18      36

> Would someone more knowledgeable than myself please clarify the
> string/GC issue?

Hope that reassured you a bit.