Almost all the discussions on this topic I've seen on the Internet are bad news so far. According to Adobe docs, only debug version of Flash player support gc() command.
The first problem is to install debug version flash player into my Firefox:
Get Debug Version
http://askubuntu.com/questions/27042/whats-the-ultimate-way-to-install-debug-flash-player-in-firefox
http://www.playerversion.com/
My current version is:
You can't tell if it is debug version just by the above information. According to this article, you can determine that by check if there is 'debug' item in the menu by right clicking, here is Adobe official site to help you with that, but I don't find the info about debug version. Any way, the former method works:
As you see, there is no 'debug' item.
And now go to the site:http://www.adobe.com/support/flashplayer/downloads.html
After installing it, check that again:
So far, we figured out the first question.
What People Say about that
The blog: Forcing Garbage Collection In Flash (AS3) said that there may be some problems on calling System.gc(), and it gave one good suggestion that is applying a try-catch clause, due to not every version FP has this function:
function callGarbageCollector():Boolean
{
try
{
System.gc()
return true;
}
catch (e:Error)
{
return false;
}
}
It also mentioned that with the old version it may takes two times invoke to take effect. It finally summed up the guide lines to de-reference the objects to let garbage collector to collect them:
Remove all event listeners.
Stop all MovieClip and sounds.
Nullify all references to every MovieClip.
Detach movie clips from their parents.
Avoid static variables or isolate them on a separate class.
Stop all timers that is being referenced by a movie clip.
And from the official article: Freeing memory, it also suggests that releasing BitmapData by callingdispose() and releasing XML data by callingdisposeXML() function.
From the article: Understanding Garbage Collection in AS3, calling gc() function manually is not recommended, just making sure that the objects are de-referenced correctly is fine
And it also use weird code to replace System.gc():
try
{
new LocalConnection().connect('foo');
new LocalConnection().connect('foo');
}
catch (e:*) { }
Memory Management in AIR / AS3 / Flash Garbage Collection also list the outlines:
Remove external references
Remove effect references
Remove any Tween listener references
Remove any function references (eg ArrayCollection Filter functions)
Remove any parent references to a component with deleteReferenceOnParentDocument
Remove all event handlers
Remove any manually created bindings
Dispose of any bitmap data
Dispose of any XML data
Kick start the garbage collector
See It in Profile
At the moment, I have followed the guide lines listed in the above blogs, to modify my codes. And monitor its memory usage in Alcon:
As the program started up, its memory usage is a little more than 46m:
After I navigate to the deepest level page, means it creates lots of MovieClips and loads lots of assets such as SWF and image and XML, it reached one peak at around 91mb:
Then go back to the home page, means all the pages's references just created would be marked as null, they are available for GC to collect, then after about 40 seconds, the curve dropped down to 51.9m:
Would be 40 seconds a too long time? I just found that it normally takes longer than 40 seconds, maybe two minutes, would that be too long?
Garbage Collection in Action
Now add the lines to proper place:
import flash.system.System;
// added by Vincent 2013-02-07
if (0 == this._pageStack.length)
{
try {
System.gc();
}
catch ($e:Error)
{
LOGGER.error("Garbage collecting fail." + $e.toString());
}
}
else
{
if ( (System.totalMemory / 1048576) > 200 )
{
try {
System.gc();
}
catch ($e:Error)
{
LOGGER.error("Garbage collecting fail." + $e.toString());
}
}
}
// end
And we can see that it really takes affect. But there is still something that were not free immediately.
Ref:
Understanding Garbage Collection in AS3
Memory Management in AIR / AS3 / Flash Garbage Collection.
Forcing Garbage Collection In Flash (AS3)
Motherfucking AS3 Garbage Collection