Garbage Collection in Actionscript3.0

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:

current-version


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:


my-current-fp


As you see, there is no 'debug' item.


And now go to the site:http://www.adobe.com/support/flashplayer/downloads.html

the-version-to-download

After installing it, check that again:

check-debug


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:

startup


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:

peak


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:

back-down


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.

result



Ref:

System - AS3

Understanding Garbage Collection in AS3

Memory Management in AIR / AS3 / Flash Garbage Collection.

Forcing Garbage Collection In Flash (AS3)

Freeing memory

Motherfucking AS3 Garbage Collection



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值