A memory leak when removing and adding an image sequencely?

The following code removes and adds an image to the screen (when the blue button is pressed).
It also prints memory usage to the terminal.

Looks like memory is increasing on every press , although the image is removed and dereferenced (lines 36-37)…
Another strange thing is that if you set image scale to be 0.25 (just un-remark lines 43-44), memory seems to stay the same…

Is there a memory leak here or is it a problem in the collectgarbage( “count” ) method?
Is it a bug in my code or in Corona’s SDK?

The memory leak in this sample is negligible, but the code here is the base for my state machine where the memory leak becomes noticible.

Appriciate any help / info on this issue,
Thanks,
EZ

[code]
– vars local
local main_sprt = nil
local img_sprt = nil
local big_btn = nil

– forward declerations
local tapCb


– init

local function init()

– main_sprt
main_sprt = display.newGroup()

– big_btn
big_btn = display.newRect( 0, 0, 128, 128 )
big_btn:setFillColor( 0, 0, 255 )
big_btn.x = 0.5 * display.contentWidth
big_btn.y = display.contentHeight - big_btn.height
big_btn:addEventListener( “tap”, tapCb )

end


– tapCb

function tapCb( evt )

– remove img
if ( img_sprt ~= nil ) then
–img_sprt:removeSelf()
main_sprt:remove( img_sprt )
img_sprt = nil
end

– insert img
img_sprt = display.newImage( “Default.png” )
img_sprt.x = 0.5 * display.contentWidth + math.floor( -50 + 100 * math.random() )
–img_sprt.xScale = 0.25
–img_sprt.yScale = 0.25
main_sprt:insert( img_sprt )

– collecg garbage
collectgarbage( “collect” )

– print memory usage
local memUsage_str = string.format( “memUsage = %.3f KB”, collectgarbage( “count” ) )
print( memUsage_str )

end

–##################################################################################################
– START
–##################################################################################################
init()
[/code] [import]uid: 9536 topic_id: 4295 reply_id: 304295[/import]

try wrapping it in

[lua]if(evt.phase==“ended”)[/lua]

does that make any difference? I’m just wondering if the event keeps any references to objects throughout the phases

i know there’s an ansca example that does memory tests of adding/removing images that is meant to work ok

[import]uid: 6645 topic_id: 4295 reply_id: 13380[/import]

No, it doesn’t make a difference (I’ve replaced the “tap” event with a “touch” event)…

But it might have something to do with the event, since the following piece of code doesn’t suffer from memory leak although the basic logic is the same (remove and add the same image over and over again):

local main\_sprt = display.newGroup()  
local img\_sprt = nil  
  
for i=1, 10 do  
  
 print( "---\> " .. i )  
 print( "main\_sprt.numChildren = " .. main\_sprt.numChildren )  
  
 -- remove img  
 if ( img\_sprt ~= nil ) then  
 main\_sprt:remove( img\_sprt )  
 img\_sprt = nil  
 end  
  
 print( "main\_sprt.numChildren = " .. main\_sprt.numChildren )  
  
 -- insert img  
 img\_sprt = display.newImage( "Default.png" )  
 img\_sprt.x = 0.5 \* display.contentWidth + math.floor( -50 + 100 \* math.random() )  
 img\_sprt.y = 0.5 \* display.contentWidth + math.floor( -50 + 100 \* math.random() )  
 img\_sprt.alpha = 0.5  
 main\_sprt:insert( img\_sprt )  
  
 print( "main\_sprt.numChildren = " .. main\_sprt.numChildren )  
  
 -- collecg garbage  
 collectgarbage( "collect" )  
  
 -- print memory usage  
 local memUsage\_str = string.format( "memUsage = %.3f KB", collectgarbage( "count" ) )  
 print( memUsage\_str )  
 local texMemUsage\_str = string.format( "texMemUsage = %.3f KB", system.getInfo( "textureMemoryUsed" ) )  
 print( texMemUsage\_str )  
  
end  

So I still wonder what can cause this memory leak and how I can prevent it from happening…
Any more ideas? [import]uid: 9536 topic_id: 4295 reply_id: 13385[/import]

I’m not seeing a memory leak in the latest build. The terminal output looks like:

memUsage = 67.074 KB
memUsage = 67.152 KB
memUsage = 67.152 KB
memUsage = 67.152 KB
memUsage = 67.152 KB
memUsage = 67.152 KB
memUsage = 67.152 KB

…and so forth regardless of how many images I create/destroy. Which build are you using? I’m using a different image for Default.png, obviously, but I don’t know why/if that would matter.

Tim
[import]uid: 8196 topic_id: 4295 reply_id: 13403[/import]

Tim,

My Mac is not with me right now, so I can’t tell you which build I’m using, only that I’ve downloaded the SDK about 2 months ago.

Is that the output of the first sample of code (with the tap event)?
Cause the second sample of code doesn’t show any memory leak for me, too.

If you don’t see any memory leak with the first sample, that’s good news.
In that case I’ll download the latest build and test it :slight_smile: [import]uid: 9536 topic_id: 4295 reply_id: 13413[/import]

Tim,

Bad news…

I’ve installed the latest SDK build: Version 2010.243 (2010.12.2)
The previous SDK build I was using was: Version 2.0.0 (2010.9.16.148)

Same results:

Copyright © 2009-2010 A n s c a , I n c .
Version: 2.0.0
Build: 2010.243

memUsage = 67.068 KB
memUsage = 67.436 KB
memUsage = 67.623 KB
memUsage = 67.764 KB
.
.
.
memUsage = 69.920 KB
memUsage = 70.061 KB
memUsage = 70.201 KB

I’m using the Default.png image taken from the StreamingVideo sample (some Corona text and images on a white background), but as you said I don’t think it matters.
I can send you the zipped project if it helps.

I’ve tested it on my iPhone 3G as well (I’ve added a text field to output the memory usage )- same results (different numbers)

I’m using a Mac mini ( 2 GHz inter Core 2 Duo, 1 GB 1067 MHz DDR3 ), Mac OS X Version 10.6.5 to build

As I said, one of the strange things is that if I scale down the image to 0.25, it doesn’t leak any more (lines 48-49)…

The reason it bugs me so much is that I’ve completed my first Corona game which is based on a state-machine I’ve created.
This state-machine has a similar mechanism like the described code (add graphic objects, remove them, add another etc.).
My game is leaking (with every state change) and I suspect it is for the same reasons…

On the same subject - is there a way or a tool to do memory profiling with Corona projects?

Here is, again, the code which causes the leak (with an added text filed to test it on devices):

[code]
– vars local
local main_sprt = nil
local img_sprt = nil
local big_btn = nil
local mem_txt = nil

– forward declerations
local tapCb


– init

local function init()

– main_sprt
main_sprt = display.newGroup()

– big_btn
big_btn = display.newRect( 0, 0, 128, 128 )
big_btn:setFillColor( 0, 0, 255 )
big_btn.x = 0.5 * display.contentWidth
big_btn.y = display.contentHeight - big_btn.height
big_btn:addEventListener( “tap”, tapCb )

– mem_txt
mem_txt = display.newText( 0, 0.5 * display.contentWidth, display.contentHeight - 32, native.systemFont, 12 )
mem_txt:setTextColor( 128, 128, 128 )

end


– tapCb

function tapCb( evt )

– remove img
if ( img_sprt ~= nil ) then
–img_sprt:removeSelf()
main_sprt:remove( img_sprt )
img_sprt = nil
end

– insert img
img_sprt = display.newImage( “Default.png” )
img_sprt.x = 0.5 * display.contentWidth + math.floor( -50 + 100 * math.random() )
–img_sprt.xScale = 0.25
–img_sprt.yScale = 0.25
main_sprt:insert( img_sprt )

– collecg garbage
collectgarbage( “collect” )

– print memory usage
local memUsage_str = string.format( “memUsage = %.3f KB”, collectgarbage( “count” ) )
mem_txt.text = memUsage_str
print( memUsage_str )

end

–##################################################################################################
– START
–##################################################################################################
init()
[/code] [import]uid: 9536 topic_id: 4295 reply_id: 13485[/import]

Not sure about this, but aren’t you autodeleting the local variables you define, by setting them to nil? I suspect that whatever reference to them later in your code just creates new global variables. [import]uid: 7356 topic_id: 4295 reply_id: 13489[/import]

Magenda,

Are you refereeing to the img_sprt I’m setting to nil?
As far as I understand, this is exactly what tells the system to free the memory…

Did you try this code on your machine?
Did you get a memory leak? [import]uid: 9536 topic_id: 4295 reply_id: 13495[/import]

Any news here?
Was anyone able to reproduce this memory leak?

Appreciate any help with this issue… [import]uid: 9536 topic_id: 4295 reply_id: 13546[/import]

Weird, I thought I sent a reply…anyway, yes I was able to reproduce the issue with the Default.png you were using, and I filed a bug for engineering to take a look.

Perhaps this is cold comfort, but the leak is limited to the Lua-part of the Corona runtime; the texture memory usage (on the C++ side) remains constant throughout, which you can monitor with system.getInfo( “textureMemoryUsed”), e.g.:

 local memUsage\_str = string.format( "memUsage = %.3f KB. texture = %.3f", collectgarbage( "count" ), system.getInfo( "textureMemoryUsed") )  

If I hear any updates on this issue that will help, I’ll let you know.

Tim [import]uid: 8196 topic_id: 4295 reply_id: 13559[/import]

Thanks Tim,

Yes, I’ve noticed that texture memory stays the same.

Is this bug thread available to us developers? Can you add a link to it? [import]uid: 9536 topic_id: 4295 reply_id: 13562[/import]

@ez123

Using your code “as is” in post #5 (within an dummy scene template I have for testing things) and with the newest Corona build, there is no leak here. Stable memory after continuous clicks… (I have used a different image though)

memUsage = 181.282 KB
memUsage = 181.142 KB
memUsage = 181.142 KB
memUsage = 181.196 KB
memUsage = 181.196 KB
memUsage = 181.196 KB
memUsage = 181.196 KB
memUsage = 181.196 KB
memUsage = 181.196 KB

memUsage = 181.196 KB
memUsage = 181.196 KB
memUsage = 181.196 KB
EDITED [import]uid: 7356 topic_id: 4295 reply_id: 13563[/import]

Thanks Magenda, for testing it :slight_smile:

Yes, it seems to have different results for different images (see Tim’s posts).

If you are not tired of trying, try to use the image I’ve mentioned (from Corona’s SDK sample) and see if you still don’t get memory leak. [import]uid: 9536 topic_id: 4295 reply_id: 13566[/import]

With the Default.png from Streaming Video sample I get:

memUsage = 181.205 KB
memUsage = 181.338 KB
memUsage = 181.525 KB
memUsage = 181.666 KB
memUsage = 181.807 KB
memUsage = 181.947 KB
memUsage = 182.088 KB
memUsage = 182.229 KB
memUsage = 182.369 KB
memUsage = 182.510 KB
memUsage = 182.650 KB
memUsage = 182.791 KB
memUsage = 182.932 KB
memUsage = 183.072 KB
memUsage = 183.213 KB
memUsage = 183.354 KB
memUsage = 183.869 KB
memUsage = 184.010 KB
Heh… try however to scale the image down after creating it: img_sprt:scale(0.1,0.1)
You again get no leak :slight_smile:

memUsage = 181.307 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
memUsage = 181.244 KB
Weird! [import]uid: 7356 topic_id: 4295 reply_id: 13570[/import]

Yeap, weird indeed… [import]uid: 9536 topic_id: 4295 reply_id: 13624[/import]

This is something I just noticed myself. after a few days trying to find where the leak was happening I have decided that somehow not all the memory is being released no matter what i do. I am about to try your scale the object trick and see if that makes a difference.

Cheers

Mike R [import]uid: 9950 topic_id: 4295 reply_id: 16963[/import]

Is it particular image format that cause problems? all PNGs or just some? [import]uid: 8353 topic_id: 4295 reply_id: 18370[/import]

Any more news on this issue? Any workarounds? It was mentioned to me to save my images as png-24 but that’s not made any difference to me.

I am noticing an increase of 3 or 4kb each time I reload a level and this is after checking and double checking and well you get the picture.

Maybe I am being too much of a perfectionist but I hate anything that I have no idea why it is not doing what I expect. :smiley:

Maybe whats being suggested here is that i shrink all the images before removing them??? :wink:

Cheers

Mike R [import]uid: 9950 topic_id: 4295 reply_id: 19847[/import]

Waiting for an update on this issue as well.
My state machine, which builds and destroys screens, leaks really bad because of this bug…

@mykyl66
Did you try it on the newest build?

EZ [import]uid: 9536 topic_id: 4295 reply_id: 19940[/import]

Yes I am working with the latest build.

Cheers

Mike R [import]uid: 9950 topic_id: 4295 reply_id: 19990[/import]