Memory Leak help needed.

Hi,
Thanks for the great SDK and sharing good stuffs from the developers.

I’m new to corona sdk and I’m confusing about the memory management behaviors of the corona.

Let’s say I’m going to make books for the kids.
And each page has a lot of PNGs and when they go to next page,
I need to remove all the PNGs , and add a lot of new PNGs for the new page.

But I noticed that when I go up the page , the memory usage is keep increasing.
So I wrote very very simple script in the main.lua

 print(string.format( "memUsage = %.3f KB", collectgarbage( "count" ) ))  
  
 d= display.newGroup()  
 print(string.format( "memUsage = %.3f KB", collectgarbage( "count" ) ))  
  
  
  
 for i=1 ,2000, 1 do  
 local dispObj\_1 = display.newImageRect( "assets/song\_1/".."bg.jpg", 480, 360 )  
 dispObj\_1.x = 240  
 dispObj\_1.y = 160  
 dispObj\_1.xScale=1.0  
 dispObj\_1.yScale=1.0  
 d:insert(dispObj\_1)  
 dispObj\_1= nil  
 end   
  
 for i=d.numChildren, 1,-1 do  
  
 local x = d[i]  
 x.parent:remove(x)  
 x=nil  
  
  
 end  
  
 print(string.format( "memUsage = %.3f KB", collectgarbage( "count" ) ))  
  
 for i=1 ,2000, 1 do  
 local dispObj\_1 = display.newImageRect( "assets/song\_1/".."bg.jpg", 480, 360 )  
 dispObj\_1.x = 240  
 dispObj\_1.y = 160  
 dispObj\_1.xScale=1.0  
 dispObj\_1.yScale=1.0  
 d:insert(dispObj\_1)  
 dispObj\_1= nil  
 end   
  
 for i=d.numChildren, 1,-1 do  
  
 local x = d[i]  
  
  
 x.parent:remove(x)  
 x=nil  
  
  
 end  
  
 print(string.format( "memUsage = %.3f KB", collectgarbage( "count" ) ))  
  
 d.parent:remove(d)  
 d=nil  
 print(string.format( "memUsage = %.3f KB", collectgarbage( "count" ) ))  
  

Very simple.
I put 2000 PNGs
and remove them.

and 2000 PNGs again.
and remove them again.
I’m expecting memory usage goes back where it started after removing them all,
but I’m getting the following result.
Is this normal behavior?
memUsage = 82.626 KB
memUsage = 82.771 KB
memUsage = 469.979 KB
memUsage = 900.267 KB
memUsage = 900.323 KB
[import]uid: 96678 topic_id: 17457 reply_id: 317457[/import]

If I were in your shoes I would put a print statement inside your clean function and see if it is getting called. It looks like you do things a little diffrent than me. [import]uid: 39088 topic_id: 17457 reply_id: 66217[/import]

I would also use a safety net, create a displaygroup that holds all the 2000 images so that when I want to remove them, removing the group would do the needful for me.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17457 reply_id: 66234[/import]

I’m new to the way Corona deals with garbage collecting as well, so this is just an observation which may or may not be valid…

[lua]for i=d.numChildren, 1,-1 do

local x = d[i]

x.parent:remove(x)
x=nil

end[/lua]

It seems that you are simple creating a reference to d[i] by creating a new local variable (x).

Shouldn’t you also be calling:

[lua]d[i] = nil[/lua]

since [lua]x = nil[/lua] is only clearing out that local variable, and not the one it was referencing? Again, I’m still learning Corona’s garbage collection so I could be wrong. [import]uid: 49447 topic_id: 17457 reply_id: 66280[/import]