[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Can userdate have references to lua objects?
- From: RLake@...
- Date: Mon, 12 May 2003 14:42:08 -0500
> This means that tables in Lua can only grow without ever shrinking back.
> Of course every time we add to containers we seem to always think of
> the growing strategies for them. But shrinking containers back to
> a more realistic size seems to be less of a concern. I wonder why?
> Sure, if I am actively adding to a container the growth strategy
> is very important. What if I am actively removing from it?
> Is this somehow less important or less frequently used case?
This is a separate problem. It is not quite the case that Lua tables
never shrink.
To start with, in your example:
---------------BEGIN
N = 100000
t = {}
print(gcinfo())
for i =1,N do -- fill up a huge table
t[i] = i
end
print(gcinfo())
for i =1,N do -- empty the table
t[i] = nil
end
collectgarbage ()
-- NOTE! At this point the table is empty but has retained all the memory
needed to store N+ entries.
-------------END
Now try this:
t[2*N] = 42
print(gcinfo())
collectgarbage()
print(gcinfo())
Tables only shrink when they are about to grow :)
Most of the time, this is ok, because tables either grow constantly and are
deleted, or grow and shrink randomly. It will only create a problem in the
case where a table is grown to a massive size, then most of the elements
are deleted, and then no new elements are added. If you are doing that, you
should probably copy the table after deleting the elements.
The Lua garbage collector cannot shrink tables, because this would affect
any iteration using next() which happened to be active; it is not possible
to know if that is the case, so the garbage collector cannot shrink tables.
However, the garbage collector can deleted entries from a table (if the
table is weak-keyed or weak-valued or both); consequently, deleting entries
cannot shrink the table either. So the only time a table can be reliably
shrunk is when it is about to be expanded (and even then only because next
() style iteration is documented as being unstable if the table is modified
by adding a new element.)