@superandi:
Yeah GC in Flash can be really annoying. Can't believe all we have to force GC is an ugly hack! Excuse me for trying to program an action game Adobe! If I could just have it get everything out of the way inbetween levels it would make things really solid. I ran into problems with GC recently too (yesterday!), so I thought I'd share the experience. Even though I'm still not sure quite what to make of it.
Ok so I am finishing up a game in AS3 and I notice now that I've really gotten into building levels that every time I load a new level I leak about 5MB! This came as a surprise to me, because I have specifically nulled my b2World, my Level object, etc... So I figured I missed a reference somewhere. Some hours later, after cleaning up a bunch of stuff and making ABSOLUTELY CERTAIN that I was not referencing my b2World somewhere I find that it's still happening! I found that creating my b2World was where the jump in memory happened, and then it would never go back down. So I though maybe Box2DAS3 had a leak, looked around at the code and I doubt it, so...
I try forcing GC with the localConnection hack method. That got rid of a few MB, which is weird because I kept restarting the level to see how much memory I would rack up (without the hack) and it got over 150MB!! So it apparently was getting rid of something with forced GC that it never did otherwise. This is tested with standalone flashplayer and in browser.
The leak is still there. So I figure if the b2World won't die quietly I will have to beat it to death! So I attempt to explicitly delete all the stuff in the b2World like so:
Code:
var next:b2Body = world.m_bodyList;
var current:b2Body;
while (next != null) {
current = next;
next = next.m_next;
world.DestroyBody(current);
current = null;
}
world.m_broadPhase = null;
world.m_contactManager = null;
world.m_bodyList = null;
world.m_contactList = null;
world.m_filter = null;
world.m_jointList = null;
This seems to help a little, but still a leak!
So the leak is still there, though apparently a little smaller?!!! So I decide to try the memory profiler in the Flex 3 Beta. I run the profiler and it says peak memory after restarting levels about 20 times is.... 21MB?????? WTF!! So it's telling me basically that I don't have a memory leak... This is within the expected memory usage for my program (everything is drawn with BitmapData, so it caches rotations and stuff). I should add that I have only 1 instance of flashplayer running at any time, no other tabs or browsers, because I know the memory readout in Flash (from System) will read out the total for all instances of flashplayer. Meanwhile my Flash app is reading out like a gazillion MB (ok it was more like 165MB). I'm assuming it has something to do with running the profiler wackyness. Even running in browser with the hack I can still get well above 150MB though.
I really don't know what to make of this, but I will probably keep restarting the level until I get like 800MB used just to see what happens at some point (I have 1GB and running XP, so that should get laggy). The only thing I can think of is that the memory readout has some weird bug. This is the code I'm using to read out memory, so I'm not adding to some variable or anything weird:
Code:
var memused:String = new String();
memused = Math.round(System.totalMemory/1048576) + " MB used";
I guess I just won't worry about it much for now. Like I said, I don't have any real conclusion except that I don't trust the System.totalMemory anymore! I don't necessarily trust the profiler either though... Hopefully this experience is useful to someone, and maybe others have stories to share, or will at some point. I would love to hear them!