lua-users home
lua-l archive

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


Ok - that makes sense.

I think the confusion lies in way we (I) think about strings.

Lumping strings in with other types of immutable objects (e.g. numbers)
sheds some light.

I suppose lightuserdatas are treated in the same manner.

After a quick check it appears they are.  Good to know.

So - as far as memory usage goes, is it correct to say that if a string
is not reachable by any means, weak table or otherwise, it will be
collected?

BTW, thanks for the help.

-Kevin

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of
RLake@oxfam.org.pe
Sent: Tuesday, February 10, 2004 3:39 PM
To: Lua list
Subject: RE: Strings and GC - was RE: Using full userdata as table
index?



Kevin Baca preguntó: 

> Forgive me, but why when a string is used as a key for a weak-keyed 
> table is it not GC'd? 

Ah, now that *is* a good question. Wim Couwenberg and I had a
fascinating correspondence about it, a while back. 

The essence of a weak-keyed table is that the key will never come back
once it is no longer reachable, so once the only reference to the key is
the table itself, the key-value pair can be removed (possibly losing the
last reference to the value as well.) So, as Wim pointed out, if you
can't (or even don't) iterate a table, all of its keys which are mutable
objects might as well be weak. In that sense, non-iterability is the
underlying feature rather than weak-keyness; the key in a non-iterable
table is weak precisely when it is a mutable object. 

However, a string is not a mutable object (in Lua). So a string can
disappear and then be recreated *exactly as though it had never gone
away*. It is therefore not safe to remove a (string)key-value pair from
a weak-keyed table even if there is no other existing reference to the
string key, because you might later recreate the string. If Lua had
mutable strings, then they would be GC'd from weak-keyed tables. 

Rici