An AS3 module I was working on was eating up memory, and a FLVPlayback video was having some trouble playing occasionally, so I decided to track what was happening and systemically improve performance.
Searching the net I found a few good sites with information regarding memory optimization and general performance optimization:
- Good stuff about closure, and some info about E4X Memory Performance in the Land of References
- Simple Method Closure in AS3 simple-method-closure-in-as3.php
- Trying to define ‘method closure’ see: Method Closure Owner (arguments.caller)
- By Stuntman, a very good list to follow : ActionScript 3 Best Practices to Improve Performance
- A good definition of the Garbage Collector: AS3 Garbage Collector Note
- Link on why to use int over uint: Types in AS3: ints not so fast, uints slow!
- The AS3 Cookbook: ActionScript 3.0 Cookbook: Chapter 1: ActionScript Basics
(Mainly culled from the above list from Stuntman)
- Use switch statements instead of a group of if/else statements. (better for performance)
- Remove all listeners ( when no longer required ). This has had a dramatic positive effect in the performance of my Framework.
- Make sure you stop and clear timers/intervals when not in use. (better for performance
- Remove/Unload unneeded assets from the stage, when done with them. ( See add destroy() Methods
- Use int instead of number when needed ( is better than uint, see above )
- When exporting your Flash change the JPEG quality to 60. ( to check all over )
- Allow the Garbage Collection to collect! to Quote from Krill: When storing references to methods what has to happen for garbage collection to act right is the objects involved must form a closed circuit. If there’s ANY reference to one of the objects in the circuit from outside then none of them will be garbage collected..
The above information about to Method Closure relates to this.
- Add destroy() Methods to Classes to remove assets and properties.
By following the techniques above systematically I was able to improve performance dramatically. Now I will add weak referenced listeners as standard practice! Also, work on the idea of having a pair of create() and destroy() Methods in each relevant Class, to implement Lazy Loading and tidy asset removal.