lua-users home
lua-l archive

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


Hi all,

I'm a bit puzzled by a problem that I've reduced to the following.
Firstly, whenever I check how much memory I'm using from within Lua,
during my tests, I always perform a full garbage collection cycle,
like this:

function GetUsedMemory()
collectgarbage("collect");
return collectgarbage("count");
end


-- Now consider this little script:


-- Tell me how much memory I'm using first
print("Memory use at start:", GetUsedMemory());

-- Fill up a table with a million items (anything, makes no difference)
foo = {};
for i = 1, 1000000 do foo[i] = "bar"; end

-- Now empty the table again
for i = 1, 1000000 do foo[i] = nil; end

-- Sanity check, shows empty table which is correct
print("   ** Start of table contents **");
for k, v in pairs(foo) do print(k, v); end
print("   ** End of table contents **");

-- So far so good... But now spot the difference...
print("Around 16meg too much:", GetUsedMemory());
foo = {};
print("Back where we started:", GetUsedMemory());


------- snip -------


When iterating over the table to print any items in the foo table, it
correctly shows no items at all. But somehow, somewhere, Lua keeps
about 16mb of memory around, as shown by the two print statements
following that.

I'm guessing this should not happen. Am I wrong? Any ideas as to why
it does happen?

I'm using a plain precompiled Lua binary for win32, and no additional
modules or whatever. At least not for this little test. I'm running on
Windows XP sp2 and fully updated. Anyone should be able to copy/paste
the above code and run it, and it probably shows the same result (can
anyone please verify this?)

I've kept my real test app running during the night. It's a memory hog
(that's part of the stress test) but the amount of memory it uses
should be fairly stable. The following morning I noticed it had
stopped, because it ran out of memory, and I think the code above
demonstrates why.


Regards,
Mark