Skip to content

Flex GC

ythy edited this page Jan 17, 2018 · 2 revisions

Garbage Collector

The garbage collector is a behind-scenes process that is responsible for deallocating the memory used objects that are no longer in use by the application.it no longer has any references to it from other active objects.

Reference counting:

When you create a reference to an object its reference count is incremented.When you delete a reference,its reference count is decremented.If the reference count of an object reaches Zero,it is marked for deletion by the garbage collector.
fatal pro:circular referencing.

Mark Sweeping:

The player starts at the root node of your application and walks through every reference on it,marking each object it finds.It then iterates through each of the marked objects,marking their children.It continues this process,it can safely deallocated.
flaw:costly.

Deferred GC and Indeterminacy

Objects will not be removed immediately when all active references are deleted,instead they will be removed at some indeterminate time in the future(from a developer standpoint).The GC uses a set of heuristics that look at RAM allocation and the size of the memory stack(among other things) to determine when to run.As a developer,you must also be aware that inactive objects will continue to execute indefinitely(until the GC deallocated it),so code will keep running,sounds will keep playing,loads will keep happening,events will keep firing,etc.

GC

In Flash 10, there is a System.gc() method you can call (but please don't, see above) - keep in mind System.gc() only works in the debugging version of Flash player 10+.
In Flash 9, there is an unsupported way to force it via an odd LocalConnection command, but it may not work in all versions.

Weak References

A weak reference is one that is not counted by the Garbage Collector(ie.it is not counted in reference counting, and it is not followed for mark sweeping).

weak references are only supported in two contexts. The first is event listeners, which is great because event listeners are one of the most common references that cause problems with garbage collection. I strongly recommend always using weak references with listeners by passing true as the fifth parameter of your addEventListener calls:

someObj.addEventListener("eventName",listenerFunction,useCapture,priority,weakReference);
stage.addEventListener(Event.CLICK,handleClick,false,0,true);
// the reference back to handleClick (and this object) will be weak.

The other place that weak references are supported is in the Dictionary object. Simply pass true as the first parameter when you instantiate a new Dictionary to have it use weak references as its keys:

var dict:Dictionary = new Dictionary(true);
dict[myObj] = myOtherObj;
// the reference to myObj is weak, the reference to myOtherObj is strong

Unsupported Way to Force GC

In my previous article on Garbage Collection in AS3 I said that the GC is indeterminate – that there is no way to know when it will run. That is not entirely true, there is a trick that will let you force the Flash player to carry out a full GC pass. This trick can be really handy for exploring Garbage Collection, and testing your applications during development, but it should never be deployed in production code because it can wreak havoc with processor load. It is also officially unsupported, so you cannot rely on it to work in updated versions of the player.

To force an immediate GC mark/sweep, all you have to do is call connect() on two LocalConnections with the same name. This will throw an error, so you’ll have to wrap it in a try/catch block.

try {
new LocalConnection().connect('foo');
new LocalConnection().connect('foo');
} catch (e:*) {}
// the GC will perform a full mark/sweep on the second call.

Again, this should only be used as a development aid.

Clone this wiki locally