Fundamental misunderstanding of memory...

I must be doing something completely wrong here, but I’ve been testing the memory profile of my game, and something doesn’t appear to be clearing out properly.

As a test, I ran this code:

for i=1,2000 do  
 myRect = display.newRect(100,100,10,50)  
 myRect:removeSelf()  
 myRect = nil  
end  

and the memusage goes up by 45kb! What am I doing wrong here? Shouldn’t everything be released after calling removeSelf and setting the variable to nil? I must be missing something… [import]uid: 62193 topic_id: 20379 reply_id: 320379[/import]

I’m facing this problem too. It gets worse if you manipulate the x and y coordinates of the display objects. I think it’s something to do with how the SDK is coded or we’re doing something wrong. [import]uid: 108204 topic_id: 20379 reply_id: 79674[/import]

Try using display.remove( myRect ) instead of removeself. Also try putting local in front of myRect.

Also did you check the memory usage a few seconds after the for loop was done? Sometimes it takes a moment to clear out of the memory. You should run a repeating timer checking the memory usage. [import]uid: 10903 topic_id: 20379 reply_id: 79677[/import]

Hi, thanks for your suggestion. It doesn’t seem to work though, at least in the simulator. The memory stays elevated, even minutes after. That includes adding the local and trying, and then also using display.remove as well.

This is the code I’m using to monitor the memory:

[code]
local monitorMem = function()

collectgarbage()
print( "MemUsage: " … collectgarbage(“count”) )

local textMem = system.getInfo( “textureMemoryUsed” ) / 1000000
print( "TexMem: " … textMem )
end

Runtime:addEventListener( “enterFrame”, monitorMem )
[/code] [import]uid: 62193 topic_id: 20379 reply_id: 79689[/import]

According to the API, the display.remove() function is declared as follows:

[lua]if object ~= nil then
object:removeSelf()
end[/lua]

So it’s not going to make any difference and it’s just an extra function call.
[import]uid: 108204 topic_id: 20379 reply_id: 79695[/import]

I think the issue is that you’re creating so many objects at once. I can’t explain why that’s the case, but if you space it out for even a milisecond between each object creation the leak disappears. Here’s the code I used:

  
local table = {}  
  
local monitorMem = function()  
   
 collectgarbage()  
 print( "MemUsage: " .. collectgarbage("count") )  
  
end  
  
monitorMem()   
timer.performWithDelay( 500, monitorMem, 0 )  
  
local function makerects()  
  
 local myRect = display.newRect(100,100,10,50)  
 table[#table + 1] = myRect  
  
 for i, obj in pairs( table ) do   
  
 display.remove( myRect )  
 myRect = nil  
 table[i] = nil  
  
 end  
  
end  
  
timer.performWithDelay( 1, makerects, 5000 )  
  

And the terminal readout I get:

MemUsage: 78.419921875
MemUsage: 79.3837890625
MemUsage: 79.4072265625
MemUsage: 79.4072265625
MemUsage: 79.4072265625
MemUsage: 79.4072265625
MemUsage: 79.4072265625
MemUsage: 79.4072265625
MemUsage: 79.4072265625
MemUsage: 79.4072265625
MemUsage: 79.4072265625
MemUsage: 79.4072265625

Maybe someone else could explain why creating the objects all at once vs spacing them out causes the leak. Or maybe were both doing it wrong. [import]uid: 10903 topic_id: 20379 reply_id: 79702[/import]

Interesting… [import]uid: 62193 topic_id: 20379 reply_id: 79706[/import]

I got a chance to implement this in my code, and it works! Thanks for the workaround. [import]uid: 62193 topic_id: 20379 reply_id: 79820[/import]

Garbage collection isn’t having time to run.
Normally you allocate some memory:

myImage = display.newImage(“blah.jpg”)
later you release it:

myImage:removeSelf() – marked the memory for being freed
myImage = nil – flag to garage collection to actually release the memory
later garbage collection runs and releases the memory.

In your case at a very rapid speed, you are allocating a block, flagging it for release, and immediatly allocating the memory again using the same pointer and you repeat this alot.

finally when the loop is done, GC runs, but it only frees the last chunk of memory.
try inserting the line:

collectgarbage(“collect”)

after you set the variable to nil and see if that changes your memory foot print. It will slow your sample down, but in reality you’re not going to create 2000 rectangles as fast as you can and immediately dispose of them.
[import]uid: 19626 topic_id: 20379 reply_id: 79827[/import]

[text]in reality you’re not going to create 2000 rectangles as fast as you can and immediately dispose of them.[/text] - My game is very fast-paced (j/k)

Actually, you are right. If you make a bunch of rectangles, then delay then go back and get rid of them, it releases all the memory. I was testing for memory leaks by creating a large amount of my in game characters and then immediately deleting them (the large amount was to exaggerate any memory leaks if they were there).

I think you’ve solved this one then, thanks guys! [import]uid: 62193 topic_id: 20379 reply_id: 79859[/import]