Page 1 of 1

Middleclass apparent memory leak ?

Posted: Sun Dec 26, 2010 1:51 pm
by GeoffW123
Hi

Newbie here, I have been experimenting a bit with Lua and the Middleclass library, an apparent memory leak with Middleclass has got me totally stumped. Is this really a leak or am I misunderstanding something.

I cut my code down to just the following lines

Code: Select all

require "middleclass"

local oPlayer = class("A Player")		-- This single line is enough to show a leak

collectgarbage("collect")
local endMem = string.format("\nMemory used after Garbage Collect = %.1f kbytes", collectgarbage("count"))
print (endMem)
If I run this code repeatedly from a Lua console with dofile("myTest.lua") I get the following increasing memory usage.
Memory used after Garbage Collect = 43.8 kbytes
Memory used after Garbage Collect = 46.0 kbytes
Memory used after Garbage Collect = 48.2 kbytes
Memory used after Garbage Collect = 50.6 kbytes

If I add the line oPlayer = nil, that makes no difference either

The other things I tried made no sense to me.

Test 1
instead of the require("middleclass") line, I pasted in the middleclas lua code so everything was in the one file, to my surprise that fixes the memory leak ?

Test 2
Back with the original code with the single require "middleclass" line, if I run that under Scite, it shows no memory leak either ?

No idea whats going on here, can anyone explain this and help out a confused Newb please ?

Regards Geoff

Re: Middleclass apparent memory leak ?

Posted: Sun Dec 26, 2010 5:06 pm
by TechnoCat
require() is overrided by LOVE right?

Re: Middleclass apparent memory leak ?

Posted: Sun Dec 26, 2010 9:07 pm
by kikito
It was indeed a memory leak.

The problem was on this line:

Code: Select all

local _classes = setmetatable({}, {__mode = "k"})   -- weak table storing references to all declared classes
The mode should have been "kv" instead:

Code: Select all

local _classes = setmetatable({}, {__mode = "kv"})   -- weak table storing references to all declared classes
The difference between requiring a file and copy-pasting it inside test.lua is that "copy-pasting" overwrites everything. This "overwrite" also "resetted" _classes to an empty table; all the references inside the previous _classes were then marked as garbage and collected.

When you require a file, on the other hand, Lua first checks whether the file has already been included. If it has already, it just returns. In other words, _classes was not overwritten on your first example.

_classes was weak only on its keys, but it was storing the classes as keys and values; as a result, the value references were never discarded by the garbage collector; each execution of test.lua added a new class to _classes.

The change has been implemented on github, and it seems to be properly liberating memory now - thanks for reporting this!

Re: Middleclass apparent memory leak ?

Posted: Sun Dec 26, 2010 9:58 pm
by GeoffW123
Hello Kikito

Wow, that was impressively quick. I wasnt even sure I was correct in saying it was a leak, so I am glad I didnt waste your time. I have tested your fix, it works for me too. Thanks for the explanation and the fix.

I wonder how much money I would have to pay Microsoft for a support contract that included fixing bugs within hours on Boxing day ? Whatever it would cost I couldnt afford it :awesome:

Thanks again, happy Xmas and New Year

Geoff

Re: Middleclass apparent memory leak ?

Posted: Sun Dec 26, 2010 10:31 pm
by kikito
Our motto is: satisfaction guaranteed,

...or else you get your money back :D