views:

560

answers:

5

How do I make Flex/ActionScript 3 objects eligible for garbage collection? What are the thumb rules? ... apologies if this was answered else where.

+2  A: 

info on garbage collector here

bunch of more info here

sergiogx
A: 

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.

greg
A: 

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)

Allan
A: 

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!

TandemAdam
A: 

Most everything there is to know about it is contained in those blog posts listed above, but the basic break down is:

  1. 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
  2. set the original object handle to "null" to help the garbage collector realize the reference is no longer active
  3. wait. :) Or use the Grant Skinner trick (linked above) to try and force garbage collection quickly.
RJ Owen
don't use that hack to force garbage collection - on Grants advice :)
Allan
Yeah - I included that hack in a presentation at 360 Flex a few years back and the Adobe Flex team members there were less than enthusiastic.
RJ Owen