从内存中删除 Object (二)
作者:jack 日期:2007-09-04
从内存中删除Object (二)
jackgun_at_126.com
看看来自 Essential.ActionScript.3.0 的一段:
To avoid unnecessary code execution in discarded objects, a program should always
deactivate objects before discarding them. Deactivating an object means putting the
object in an idle state where nothing in the program can cause it to execute code. For
example, to deactivate an object, we might perform any or all of the following tasks:
• Unregister the object’s methods for events
• Stop all timers and intervals
• Stop the playhead of timelines (for instances of movie clips created in the Flash authoring tool)
• Deactivate any objects that would become unreachable if the object, itself, became unreachable
这样先前的代码改成:
Test 类 加一个停止 Timer 的 function
package {
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Test extends Sprite {
private var time : Timer;
private var i : int = 0;
public function Test() : void {
time = new Timer(500, 0);
time.addEventListener("timer", runmovie);
time.start();
}
private function runmovie(event : TimerEvent) : void {
i++;
trace("---testing....." + i);
}
//加一个停止 Timer 的function
public function stopTimer() : void {
time.stop();
}
}
}
移除 testobj 前先停止 Timer
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.system.System;
import flash.events.TimerEvent;
public class MainDocumentClass extends MovieClip {
public var testobj : Test;
private var time : Timer;
private var i:int=0;
public function MainDocumentClass() : void {
testobj = new Test();
addChild(testobj);
time = new Timer(500, 0);
time.addEventListener("timer", runmovie);
time.start();
}
private function runmovie(_event : TimerEvent) : void {
i++;
if(i==6){
//删除 testobj 时先停止 Test 的 Timer
testobj.stopTimer();
trace("移除 testobj");
removeChild(testobj);
testobj=null;
}
trace("==" + System.totalMemory);
}
}
}
这样就可以等待回收机制把它干掉了,至于什么时候干掉…天知道…
要反复使用同一个类时最好用单例模式。
回复
]flash的回收机制,好象是说如果没有引用指向该对象,那么系统会自动回收.
cs~ cs~ cs~暴头,暴头........
上一篇
下一篇

文章来自:
Tags: