Potential Serious Memory Leaking Issue

I’m not sure if there is already a bug tracker for this one, please let me know if there is.

There seems to be a problem where memory is leaking, though there’s really no way to track where it is coming from. It was discovered by myself, and others as well (as there are other forum threads regarding the issue), from using the collectgarbage(“count”) function.

The problem is, when removing a group of objects (so it commonly occurs when switching scenes), collectgarbage(“count”) continues to report an increase in memory with every scene change. It just keeps climbing without leveling out whatsoever.

This is the case even after double, triple, and quadruple checking that no display objets are being left behind. That all are removed and nil’d out properly, and that all timers and transitions are cancelled and also nil’d out. Memory seems stable while within the scene and interacting with the app, however, when a “scene change” takes place, memory climbs.

With smaller scenes, the number increases slightly, however, with some scenes it goes up pretty significantly (sometimes 50-100k!) and never goes back down. Like I said, it happens even with small scenes where it is easy to see that I have cleaned everything up properly.

It should also be noted that Texture Memory seems perfectly fine throughout. It is higher on bigger scenes, and when a scene is unloaded, it goes back down, etc.

Here are a couple more forum threads that discuss the issue:

http://developer.anscamobile.com/forum/2011/01/31/memory-problems-using-director-class

http://developer.anscamobile.com/forum/2010/12/08/memory-leak-when-removing-and-adding-image-sequencely


Please let me know when this bug is officially documented.

Thank you very much,

Jonathan Beebe [import]uid: 7849 topic_id: 7539 reply_id: 307539[/import]

Can you provide code that demonstrates the problem? [import]uid: 12108 topic_id: 7539 reply_id: 26762[/import]

@jhocking: Unfortunately I can’t share my client’s code here, and don’t have time to rewrite an example, but it can be reproduced if you add your scene’s objects to a group, then remove all items in the group and load up items into another group (or just use the Director class).

In your main.lua file, add an enterFrame listener that calls print( collectgarbage(“count”)) and you should see what I’m talking about. According to one of the links in my first post, I guess it only happens with some images… so you may or may not be able to reproduce it.

You can see a working example of it here:

http://developer.anscamobile.com/forum/2010/12/08/memory-leak-when-removing-and-adding-image-sequencely [import]uid: 7849 topic_id: 7539 reply_id: 26764[/import]

it can be reproduced if you add your scene’s objects to a group, then remove all items in the group and load up items into another group

Well that’s precisely what I do in this example, and I’m pretty sure the only memory leak is a tiny one from a bug with Corona’s physics:

http://developer.anscamobile.com/code/object-oriented-sample-game-framework

I can’t double check it right now, but as I recall if you just comment out the addBody lines then the tiny memory leak goes away.

I guess it only happens with some images… so you may or may not be able to reproduce it.

Oh well you should have mentioned that at the very beginning. Is there any chance you could isolate an image that is causing this issue (basically, test them one at a time until you get the memory leak, which will hopefully be the very first image you test) and then explain how exactly that image was created?

I would guess there is something subtle going on with PNGs encoded a certain way. Different tools produce slightly different png files, and it may be that Corona doesn’t like the specific tool you’re using. [import]uid: 12108 topic_id: 7539 reply_id: 26791[/import]

if someone sends me code i can look at it when i get to it.

is it substantial amount of memory

do you have references still pointing to destroyed objects? [import]uid: 24 topic_id: 7539 reply_id: 26824[/import]

carlos, did jon ever send you his problem? I’m curious if this got resolved. [import]uid: 12108 topic_id: 7539 reply_id: 29177[/import]

@jhocking: This bug is SQUASHED as of Daily Build 318 – Great job Ansca! [import]uid: 7849 topic_id: 7539 reply_id: 29214[/import]

That’s good. So what was causing the problem? [import]uid: 12108 topic_id: 7539 reply_id: 29224[/import]

It had to do with child objects in a group not getting removed when a group was (so this affected a lot of external libraries, such as movieclip, ui, and a whole lot of others) and it also had to do with the corresponding listeners attached to objects not getting removed as well.

All is fixed now though and working smoothly as far as I can tell. [import]uid: 7849 topic_id: 7539 reply_id: 29226[/import]

hi john I wanna ask about the memory leaking part because I still got problem. Changing game from one scene to another scene and the memory still increase, I check it using collectgarbage(“count”), and my texture memory perfectly fine because it can goes from big to small size. I create the game in windows platform and I use director 1.2, and using corona sdk build 505. And I wonder the way you can delete all your child objects?? [import]uid: 64917 topic_id: 7539 reply_id: 43353[/import]

my game no UI buttons, no pyhics, no sprite, just img with drag and drop function but now the memory leaking issue still be my problem. I’ve tried to nil, remove listener and removeSelf all objects and my memory checking still increase the size between each scene,and I wonder what is goin on with my game though… [import]uid: 64917 topic_id: 7539 reply_id: 43503[/import]

Can you post your memory count function ? [import]uid: 39088 topic_id: 7539 reply_id: 43514[/import]

[lua]local monitorMem = function()
collectgarbage(“collect”)
print(“System Memory : " …(collectgarbage(“count”)/1000)…” MB")
end

local timerCheck = timer.performWithDelay(100, monitorMem, -1)[/lua]

I tried the same way like yours to see the memory and it’s the same problem I had [import]uid: 64917 topic_id: 7539 reply_id: 43698[/import]

I had this same problem for a while with memory and switching scenes. But I think I got it nailed so let me share with you what I did to get my self out.

First off I think there is something wrong or something I was doing wrong with the GC, Memory Count function. Below is some code to demonstrate what I mean if you run this main.lua the memory gradually rises and then falls. now I didnt run it in runtime in my game I used it when changing scenes and other times during gameplay that i wanted to know the memory level.

[lua]local function memory (event)

print(“System Memory : " …(collectgarbage(“count”)/1000)…” MB")
print(“Texture Memory : “…(system.getInfo(“textureMemoryUsed”)/1000000)…” MB”)

end

Runtime:addEventListener(“enterFrame”,memory)[/lua]

Now I wasnt sure if this was because of setting it in a Runtime function or what. So I went to search for another GC,memory count function. What I found was almost perfect. Props to crsmn

Here is the code [lua]local function memCheck (event)

collectgarbage(“collect”)

print(“System Memory : " …(collectgarbage(“count”)/1000)…” MB")

end

local timerCheck = timer.performWithDelay( 100, memCheck, -1 )[/lua]

This code reads stable memory in a main.lua. So now that I had stable reading memory code.

I put it in my game and guess what I still had memory a massive memory leak. After 5 screen changes I went roughly from 150 KB to 420 KB. :frowning: So off I went to figure out what the heck was going on.

[edit] this was my bad as my ui buttons were global.
One, I had to nil out all my buttons that used the UI class.

Two, instead of using, require("") in every scene I put them in the main.lua and that was it.

Three, nil out function __new__ if you are using director. Every scene is in this new function. I found nilling this out will make big diffrence. Also nilling out any function that you declare with out using local first. I even nil out my clean function. I dont notice a memory increase in changing scenes if you declare your function local though.
Those were three things that I did to get rid of my Memory leaks, well ontop of Removing groups, timers and Runtimes.
And thats it. I have a very small memory leak now a few bytes not Kilo Bytes but Bytes. :slight_smile: Sorry for my noobness for all you advanced devs out there. I hope this helps some people out with their memory problems. [import]uid: 39088 topic_id: 7539 reply_id: 43378[/import]