I'm currently working on a very large Flash platformer game (hundreds of classes) and dealing with an issue where the game slowly grinds to a halt if you leave it on for long enough. I didn't write the game and so I'm only vaguely familiar with its internals. Some of the mysterious symptoms include,
- The game will run fine for a determinate amount of time (on a given level) when all of a sudden it will exponentially start leaking memory.
- The time it takes for the game to hit the point where it leaks exponentially is shorter when there's more sprites on the screen.
- Even when nothing is being visibly rendered to the screen, the game slows down.
- The game slows down faster with more frequent sprite collisions.
- Completely disabling the collision code does slow down the degradation but doesn't prevent the game from eventually dropping frames.
Looking at the source and using Flex profiler, my prime suspects are,
- There are many loitering objects, especially
WeakMethodClosure
, taking up large amounts of memory.
- The program makes extremely extensive use of weak event listeners (dozens dispatched per frame).
- BitmapData is being copied every time a new sprite is created. These are 50x50 pixel sprites that spawn at about 8 sprites per second.
I know it's next to impossible to tell me the problem without seeing the source, so I'm just looking for tidbits that might help me narrow this down. Has anybody experienced this evasive performance degradation in their own projects? What was the cause in your case?
You should consider object pooling if you have a lot of creation/destruction going on, especially with heavy objects like bitmapdata.
see Object Pool class
I recently completed a optimization of an large project.
And I can give you some architectural advices:
- Main principle – try do as little as possible function / events calls
- Get rid off all but one
onEnterFrame / onInterval / onTimer cycles. Do everything you
need in one general event call.
You probably will need a lot of static arrays for storing processed object
references.
- Do your graphics / render stuff also in one main loop
- Try use big (probably prerendered) canvases instead of small sprites / bitmaps.
Usually it usable for backgrounds. But also working good for a smaller objects
(like trees, platforms, etc)
- Rid off small bitmap resources, assemble it to one tile-sheet and draw your stuff
directly from it, through source–rect property
I hope its helps you!
Memory leaks – such headash.
P.S. Test your game in different browsers, IE – most leaking of all, sometimes it don`t clear memory every after refresh.