AS3 weak reference & GC

来源:互联网 发布:mac断开vpn不能上网了 编辑:程序博客网 时间:2024/06/05 10:04

一下内容摘自其他blog,并附有连接,讲的很好,希望有对不了解weak reference的有所帮助,当然,主要针对自己

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).This means that if the only references remaining an object are weak,itwill be available for collection on the next GC sweep.References associated with event listeners are often forgotten by developers,which normally results in the listenernever being removed from memory.This is why weakly referenced event listeners are so handy in As3-if you forget toremove the listener,you will not impede the Garbage Collector's ability to collect the object.Recommended:someObj.addEventListener("eventName",myFunc,false,0,true);set the fifth parameter to true;http://gskinner.com/blog/archives/2006/07/as3_weakly_refe.html

AS3 垃圾回收

Garbage Collectordef:The garbage collector is a behind-scenes process that is responsible for deallocating the memory used objectsthat are no longer in use by the application.Inactive obj: it no longer has any references to it from other active objects.Two method: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,markingeach object it finds.It then iterates through each of the marked objects,marking their children.It continuesthis process,it can safely deallocated.flaw:costly./********************************************************************************************************************************/Deferred GC and IndeterminacyObjects 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.


0 0
原创粉丝点击