00:00
00:00
WarpZone

Age 44, Male

Joined on 1/26/05

Level:
14
Exp Points:
2,076 / 2,180
Exp Rank:
29,044
Vote Power:
5.62 votes
Rank:
Police Officer
Global Rank:
13,257
Blams:
268
Saves:
454
B/P Bonus:
10%
Whistle:
Bronze
Trophies:
1
Medals:
717

AS 3.0 Garbage Collection DOESN'T suck!

Posted by WarpZone - October 5th, 2007


Just when I thought Flash's garbage collection was inherently flawed, I suddenly figured out what I and everybody else was doing wrong.

The symptom of poor Garbage Collection pretty obvious, and very painful. I first saw this in the Bloons turret defense game a long time ago. Basically, the game gradually slows down and then suddenly speeds up, over and over again.

It turns out, Flash 9's garbage collection is just FINE. You just need to know how to use it:

As you know (if you're using AS 3.0 anyway) a typical main loop looks like this:

// Initialize main game timer
var gameTimer:Timer = new Timer(20);//20 ms = 1/50 second!
gameTimer.addEventListener(TimerEvent.
TIMER, gameMainLoop);
gameTimer.start();

//Main game loop
function gameMainLoop(event:TimerEvent):void {
//Game code goes here!
}

You set up a timer event listener, and then when the listener fires, your code gets run. In my example, I'm doing it every 20 milliseconds, which works out to 50 frames per second. So you'd think the natural thing to do would be to set the agme's Frame Rate to 50 fps so you can see every frame, right?

WRONG!

You need your main timeline's refresh rate (or I guess stage.framerate if you're doing it in code) needs to be LESS THAN the ammount of time between refreshes. So, for example, in my game, I used the timer code above to update my game state 50 times per second, but my SWF only updates the graphics on the screen 40 times per second. The game runs a lot smoother now.

Why?

Because it's using that extra fraction of a second's worth of downtime after every screen update to run Garbage Collection!

So, that explains the "uneven" frame rate issues I was having with my game. It runs silky-smooth now! Next time I see anyone's game on Newgrounds and it seems to suffer from GC issues, I'll be sure and link them to this post, so they know what's going on.

And I'm still a Flash 3.0 noob, myself, so if I got some of the technical details wrong in this post, and you know better, go ahead and post a Reply below.

Update: Looks like Bloons Tower Defense has already been updated to address the GC issue. They fixed it so you can't cram as many tack towers into the corners, too... oh well, just trust me, at one time, it was the poster child for GC problems.


Comments

Comments ain't a thing here.