3

Yesterday when playing a game (Balloons Tower Defense 3) I noticed that the game started to slow down over time and then randomly speed up very quickly. Looking at the memory usage dropping each time I assumed it was the GC running.

As a legitimate question, is there anyway to force Flash to GC from, say, Firefox? Perhaps there's an addon or a command you can run that will do this? I know that you can do System.gc() but this game isn't mine so I can't modify the source with a "Click here to GC" button.

Community
  • 1
  • 1
TheLQ
  • 14,830
  • 14
  • 69
  • 107
  • 3
    One shouldn't have to do this. If it's slow, this won't make it faster. Triggering GC more frequently likely *won't* improve performance, it will burn time walking the whole heap collecting only a few unused items. –  Jul 11 '11 at 14:23
  • @delnan All I know is that the game slowed down over time, then randomly speed up. This slowness lasted several levels until the GC ran, then it rapidly sped up. GC seemed to really help – TheLQ Jul 11 '11 at 14:26
  • I'm not disputing that. But you can only collect this frequently before the overhead of another collection beats its benefit, and this sweet spot is likely where the GC's default behaviour already sits. Imagine the extreme case, a collection per allocation. It would keep memory usage as low as possible, but it would spend 90% of the time running the GC. –  Jul 11 '11 at 14:29
  • @delnan I wasn't going to be clicking "Run GC" all the time, just when it got slow. At max it would be once per level (there's a ton of objects created per level: balloons + shots). – TheLQ Jul 11 '11 at 14:32
  • Interesting to note that on the later levels of "Bloon TD" they joke about the speed of the game. "It's not the game slowing down, it's your mind speeding up." and they even compare it to Bullet Time if memory serves me. I'm not sure if Garbage Collection is the issue here, but rather the game puts too many objects on the screen. – JeffryHouser Jul 11 '11 at 14:59

2 Answers2

4

There isn't a way, and really that's not what you need anyway. Forcing the GC to run will actually make things worse. Those random slowdowns you see might actually be the GC running already.

The way you eliminate those slowdowns is to program in such a way that you are never allocating or deallocating memory inside an update function. Make all the references you need up front, never null anything (as that makes it available for collection), and always reuse objects while in the main game loop. There's plenty of time after the game is over to cleanup or re-instantiate.

scriptocalypse
  • 4,942
  • 2
  • 29
  • 41
1

as far as i know, there's no way to force GC in actionscript. not from the code itself or the surrounding browser.

* edit *

ok, there's a way - but you shouldn't use (and can't in the release version of the flashplayer) it.

more, here: Force Garbage Collection in AS3?

Community
  • 1
  • 1
Philipp Kyeck
  • 18,402
  • 15
  • 86
  • 123
  • You can, but you should't. :) – Nate Jul 11 '11 at 14:57
  • The original question links to http://stackoverflow.com/questions/192373/force-garbage-collection-in-as3 which makes note of System.gc(); which is ActionScript code to force garbage collection. [Although only in the debugger player] – JeffryHouser Jul 11 '11 at 15:01
  • ah... ok, didn't know of the System.gc() method - but because of all the points mentioned over here http://stackoverflow.com/questions/192373/force-garbage-collection-in-as3 it's better that i never knew ;) – Philipp Kyeck Jul 11 '11 at 15:30
  • System.gc only works in debug, not in release version of flash player. – J_A_X Jul 11 '11 at 15:47
  • As a rule, if you need to force gc, you're doing it wrong. time to refactor your design. :) – Nate Jul 11 '11 at 18:06