内存使用,并在AS3对象进行垃圾回收 [英] Memory usage and garbage collection of Objects in AS3
问题描述
我想知道关于对象
键入特别是当它涉及到垃圾收集在Flash。
我知道,项目将准备进行垃圾回收在这样的情况:
//创建
变种AR:数组= [];
变种MC:影片剪辑=新的MovieClip();
mc.addEventLisntener(等等,等等);
ar.push(MC);
的addChild(MC);
//杀&放大器; GC
ar.splice(0,1);
mc.removeEventListener(等等,等等);
removeChild之(MC);
但如何/会的对象
让垃圾收集的情况如下图所示。
说我有这样一个功能,我的课 MartysMC
我解析对象
到
包
{
进口的flash.display.MovieClip;
公共类MartysMC扩展影片剪辑
{
/ **
*更新此
*参数OBJ一个包含对象的键/值对重新present新的属性值
* /
公共功能的更新(OBJ:对象):无效
{
变种我:字符串;
对于(i的OBJ)
{
这个[我] = OBJ [I]
}
}
}
}
现在我利用这个功能就像这样:
VAR MMC:MartysMC =新MartysMC();
VAR数据对象:对象=
{
X:10,
Y:34,
阿尔法:0.6
};
mmc.update(数据对象);
发生数据对象
什么?这是否会得到垃圾从这里收集的?即便如此,何谈在此行的目的:
mmc.update({X:15 Y:18,店名:马蒂});
要看看会发生与GC可以使用的词典与弱引用要cheeck作为重点设置为true,并使用对象:
变种D:字典=新词典(真)
D [myObject的] =什么
当对象将不能再使用它会从字典中删除。
下面一个完整的样本根据你的例子在wonderfl: http://wonderfl.net/c/e9W4
你看,很快双方的目标都被垃圾回收。
I want to know about the Object
type specifically when it comes to garbage collection in Flash.
I know that items will be ready for garbage collection in situations like this:
// create
var ar:Array = [];
var mc:MovieClip = new MovieClip();
mc.addEventLisntener(blah, blah);
ar.push(mc);
addChild(mc);
// kill & gc
ar.splice(0, 1);
mc.removeEventListener(blah, blah);
removeChild(mc);
But how/will an Object
get garbage collected in situations like below.
Say I have a function in my class MartysMC
that I parse an Object
through:
package
{
import flash.display.MovieClip;
public class MartysMC extends MovieClip
{
/**
* Updates this
* @param obj An object containing key/value pairs to represent new property values
*/
public function update(obj:Object):void
{
var i:String;
for(i in obj)
{
this[i] = obj[i];
}
}
}
}
And now I make use of this function like so:
var mmc:MartysMC = new MartysMC();
var dataObject:Object =
{
x: 10,
y: 34,
alpha: 0.6
};
mmc.update(dataObject);
What happens to dataObject
? Will this get garbage collected from here? Even still, what about the object in this line:
mmc.update({x:15,y:18,name:"marty"});
To see what happen with the GC you can use a Dictionary with a weak reference set to true and using the object you want to cheeck as a key:
var d:Dictionary = new Dictionary(true)
d[myObject] = whatever
when the object will not be longer available it will be delete from the dictionary.
here a complete sample based on your example at wonderfl : http://wonderfl.net/c/e9W4
you see that very quickly both of your object have been garbage collected.
这篇关于内存使用,并在AS3对象进行垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!