Posted on February 19, 2008
What is that annoying AS3 Hiccup?
I’m feverishly putting the finishing touches on a game I have been building off and on, in my spare time, for about 4 months (how many prepositional phrases can he cram into one sentence?). The engine is tile-based, and I am using blitting to a single display object. I have the game loop running on an enterFrame event, and am using the built-in event model to call my render() functions, as well as a host of other event based managers that handle sounds, onscreen messages, etc. I have specifically created functions at the end of each level to clean up objects and prepare them for garbage collection. I am also creating all of my listeners with a weak reference.
I’m finally down to play testing the game, and what do I find? 2 major problems. The first is an annoying screen hiccup that occurs every now and them. The Electrotank guys seem to think it is garbage collection, but I can’t be sure. Even if it is, how do I fix it? It doesn’t make the game unplayable, but it is frickin’ annoying to say the least. The second problem is much worse. I have 30 levels in the game. Each level ramps up with more and different enemy to contend with, new mazes, etc. Last night, I uploaded a fresh version to a secret area on the site to test Mochiads and Highscores. I played to about level 24, then my browser completely died. I was about to try it again, but my son needed a bath. After the bath we played with Legos, read some books, and I never got back to re-testing the game.
I had time to sleep on it though, and I now have a plan of attack.
1. I am going to attempt a double buffer the render (thank for the idea, Squize).
2. I am going to remove the update() and render() events and call loops instead
3. I am going to remove the weak reference from the listeners. I think this might be causing the garbage collector to fire up more often then needed.
4. I am going to replace the enterFrame event with a Timer event.
The Browser Crash
1. This is probably a memory leak of some type. I hope that by fixing the problems with the hiccup, I might effect this.
2. I will pour through my code and create a global object for as many local objects as I can find. For instance, if I am creating a new object type in a loop temporarily on each iteration, I will create a version of that object outside the loop one time instead of every frame and our iteration. – This might also help the hiccup.
3. I will need to profile the memory usage and see where and when it jumps up and doesn’t go back. This will be time consuming, but it is probably the best way. Since there are not any good AS3 profilers out there, I will need to manually time and log some data. Scott Jeppesen just created a Logger in Air that I will utilize to write my profiling entries to.
Anyway, that is my current plan of attack. I hope it works. If you have any suggestions, please feel free to send them along.
*** Update ***
Chris Cutler, a co-flash head, asked me what I meant by
taking out the weak references above. I just have a theory that all of the weak
references might be causing extra reference object garbage to collect, it
turned out that the events are not the problem anyway, so I will keep
looking and post an update later. I am currently looking at all of the local
objects I create in iterations. Those might prove to be much more important
than anything else.