Potential Serious Memory Leaking Issue

13 replies [Last post]
jonbeebe
User offline. Last seen 6 years 40 weeks ago. Offline
Joined: 26 Jul 2010

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

Replies

jhocking
User offline. Last seen 7 years 45 weeks ago. Offline
Joined: 4 Dec 2010

Can you provide code that demonstrates the problem?

jonbeebe
User offline. Last seen 6 years 40 weeks ago. Offline
Joined: 26 Jul 2010

@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

jhocking
User offline. Last seen 7 years 45 weeks ago. Offline
Joined: 4 Dec 2010

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.

carlos m. icaza
User offline. Last seen 8 years 24 weeks ago. Offline
Alumni
Joined: 22 Jun 2009

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?

jhocking
User offline. Last seen 7 years 45 weeks ago. Offline
Joined: 4 Dec 2010

carlos, did jon ever send you his problem? I'm curious if this got resolved.

jonbeebe
User offline. Last seen 6 years 40 weeks ago. Offline
Joined: 26 Jul 2010

@jhocking: This bug is SQUASHED as of Daily Build 318 -- Great job Ansca!

jhocking
User offline. Last seen 7 years 45 weeks ago. Offline
Joined: 4 Dec 2010

That's good. So what was causing the problem?

jonbeebe
User offline. Last seen 6 years 40 weeks ago. Offline
Joined: 26 Jul 2010

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.

seaweedbread
User offline. Last seen 4 years 20 weeks ago. Offline
Joined: 27 May 2011

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??

Method Mobile
User offline. Last seen 21 weeks 14 hours ago. Offline
Joined: 18 Feb 2011

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.

1
2
3
4
5
6
7
8
local function memory (event)
 
        print("System Memory     : " ..(collectgarbage("count")/1000).." MB")
   print("Texture Memory    : "..(system.getInfo("textureMemoryUsed")/1000000).." MB")
 
end
 
Runtime:addEventListener("enterFrame",memory)

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

1
2
3
4
5
6
7
8
9
local function memCheck (event)
 
        collectgarbage("collect")
       
        print("System Memory     : " ..(collectgarbage("count")/1000).." MB")
      
        end
 
local timerCheck = timer.performWithDelay( 100, memCheck, -1 )

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. :( 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. :) Sorry for my noobness for all you advanced devs out there. I hope this helps some people out with their memory problems.

seaweedbread
User offline. Last seen 4 years 20 weeks ago. Offline
Joined: 27 May 2011

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....

Method Mobile
User offline. Last seen 21 weeks 14 hours ago. Offline
Joined: 18 Feb 2011

Can you post your memory count function ?

seaweedbread
User offline. Last seen 4 years 20 weeks ago. Offline
Joined: 27 May 2011

1
2
3
4
5
6
local monitorMem = function()
collectgarbage("collect")   
    print("System Memory     : " ..(collectgarbage("count")/1000).." MB")
end
 
local timerCheck = timer.performWithDelay(100, monitorMem, -1)

I tried the same way like yours to see the memory and it's the same problem I had

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.