views:

552

answers:

4

What is the best way to Free Memory in Flash ? Does it have a Garbage Collector ? How to invoke that GC ? How to make Objects applicable for Garbage Collection ?

I have a Website which displays a lot of charts. I observe that the memory that the IE uses while we display these charts continues to increase and sometimes reaches around 500 Mb. I always reinitialise unused Objects to NULL.

+3  A: 

AS3 has a built-in GC. Arguably the best resource on how it works are Grant Skinner's articles on the subject:

http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html

You can use the Flex profiler (Flex Pro only) to get a feel for how the garbage collection works. The profiler displays what objects are currently in memory and has an option to force garbage collection at the click of a button, making it easier to determine which of your objects are eligible for garbage collection at a given time.

Stiggler
A: 

One tip I can give you is to remove Event Listeners when they are no longer needed - for instance, if you are calling a WebService to retrieve some static data, and use a ResultEvent Listener, once that Event Listener is handled, call removeEventListener to free up the memory it is using. There are many other tips out there for handling memory use.

Eric Belair
+2  A: 

Just setting objects references to NULL isn't always enough. If you created any event listeners that reference that object (or if that object itself is registered as a listener) then it will still be referenced somewhere.

One way to avoid issues like that with listeners is to use weak references. addEventListener takes an optional argument that lets you tell it to use a weak reference. This will allow the object to be garbage collected if it's not referenced anywhere other than a weak-referenced event listener (or other weak references).

Herms
+1 for weak references. My experience has been that there are very few cases where you actually need a strong reference.
cliff.meyers
Weak references can cause some weirdness if you're not careful. Weak references and anonymous functions can have issues. Someone on here had an issue related to that a while ago.
Herms
+1  A: 

In many cases you can free Flex objects from the heap but the Flash VM won't give the memory back to the OS. Thus you have to code to a memory "plateu".

http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3-with-air/

sdelap