How do I make Flex/ActionScript 3 objects eligible for garbage collection? What are the thumb rules? ... apologies if this was answered else where.
Basically you need to remove all of the objects event listeners and delete all references to it making it completely un-available to the rest of your application.
removing event listeners is fairly intiutive with *.removeEventListener(event type, function)
and to remove a references use delete(varName)
.
doing the above will work most of the time but there are still some cases where this is not enough and to really take advantage of garbace collection you need to incorporate supporting practices in all areas of your code, not just when you want to remove something.
zombiegx posted your two greatest assets when looking for more information.
http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html
This is the best document I have found regarding garbage collection. In short VM2 uses a technique called Mark Sweeping. It basically traverses every object of your application like a tree and marks them. Any objects no longer marked can be deallocated. This process happens over a number of frames and not all at once.
Therefore for something to be eligible for garbage collection there must not be any references to it, so for example it must be removed from display list, no other objects holding a reference.
One thing to pay attention to is weak references. Dictionary objects for example can make use of weak references by passing in a boolean. What this means is when an object is placed in a dictionary that has been set to use weak references it will not count as a reference and therefore be eligible for garbage collection (assuming there are no other references to it)
Definitly read this: Grant Skinner - Resource management for as3. That will tell you all you need to know about FlashPlayers garbage collector, and show you a bunch of tips. Everyone using as3 should read it!
Most everything there is to know about it is contained in those blog posts listed above, but the basic break down is:
- remove all object references. This includes removing event listeners or using weak references, as stated above, and making sure the object in question isn't in any collection object
- set the original object handle to "null" to help the garbage collector realize the reference is no longer active
- wait. :) Or use the Grant Skinner trick (linked above) to try and force garbage collection quickly.