开发者

AS3: Marking a variable for garbage collection, null or delete?

If you have a variable that is a MovieClip, Sprite, Str开发者_运维问答ing, Number, int, uint, or a Boolean, do you remove it's reference by adding:

myVariable = null;

OR

delete(myVariable);


setting it to null is sufficient to remove a reference to the object it contained.

Note that the syntax of the delete operator is delete object.member, which removes member as a key from object, and as such will remove a reference to whatever it contains.

note that GC can only occur once all references to a given object are removed.


use null

delete won't work on variables that are not defined dynamically.

and always remember / never forget ... that you're nulling the reference NOT clearing the memory. meaning removing 1 reference is not always enough, you need to remove all references.

example:

var a:Object = {};
var b:Object = {};
a.name = "Alpha";
a.o = b;
b.name = "Beta";
b.o = b;

trace(b.name);
trace(a.o.name);
b = null;
trace(a.o.name);
trace(b.name);

while b doesn't exist anymore, a.o (which is the object previously known as b) is still around.

so beware!!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜