Middleclass apparent memory leak ?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
GeoffW123
Prole
Posts: 2
Joined: Sun Dec 26, 2010 1:32 pm

Middleclass apparent memory leak ?

Post 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
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: Middleclass apparent memory leak ?

Post by TechnoCat »

require() is overrided by LOVE right?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Middleclass apparent memory leak ?

Post 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!
Last edited by kikito on Sun Dec 26, 2010 10:04 pm, edited 1 time in total.
When I write def I mean function.
GeoffW123
Prole
Posts: 2
Joined: Sun Dec 26, 2010 1:32 pm

Re: Middleclass apparent memory leak ?

Post 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
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Middleclass apparent memory leak ?

Post by kikito »

Our motto is: satisfaction guaranteed,

...or else you get your money back :D
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 1 guest