Garbage collection with SWF loading
I ran some performance tests today. I made an interesting discovery. When you load a SWF that has elements exported for ActionScript, the class definitions of these elements are stored in the application’s domain and take up memory (we already knew that). But whether you load these definitions into the SWF’s context or your main application’s context will determine whether that memory is ever released.
I tried loading SWFs using the Loader class and unloading them immediately after the load completes. These SWFs contained symbols that were exported for ActionScript (linkage). When providing a LoaderContext with its applicationDomain property set to ApplicationDomain.currentDomain, even if you unload the SWF, memory of that symbol definition is never released. However, if you do not provide one, when you unload the SWF, memory is released.
Conclusion: if you are to use linked library symbols in your projects, avoid loading it in the application’s domain for performance reasons. In one of my projects, I used it intensely and my application lagged and crashed after a few hours of use (could be much less depending on the size of the symbols that you have).
You rock. Thanks for posting this, it confirms results I just discovered myself. It can be really tricky to get swfs to unload (see gskinner’s articles on the subject), there’s a recipe of different odds and ends that need to be tweaked to reliably unload a swf.
The story’s a little different in Flash 10, where swfs unload more easily (and there’s a new API to help), but your above observation about using the app’s currentDomain still holds in Flash 10.
I’ve been trying to understand this issue completely for some time now and I wonder if you could elaborate a little on this. Specifically, how does the application domain relate to the linked move clips? And how are you testing that they have or haven’t been removed from memory? How do you know?
Thanks in advance for any more light you can shed…
I posted a detailed entry on the use of application domains.
https://annafilina.com/blog/application-domain-and-external-swf-loading/
Flex Builder’s profiling tools (comes with pro version) are very helpful in determining what is loading and unloading at runtime.