从内存中删除 Object  (二)

从内存中删除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); 
           } 
      }
}

   
这样就可以等待回收机制把它干掉了,至于什么时候干掉天知道
要反复使用同一个类时最好用单例模式。




[本日志由 jack 于 2007-09-04 06:59 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: as3 内存 删除 回收
相关日志:
评论: 2 | 引用: 0 | 查看次数: -
回复回复jack[2007-09-13 09:54 AM | del]
google 一大把...
回复回复虫子[2007-09-12 05:39 PM | del]
Essential.ActionScript.3.0 ?什么地方看的.现在有电子版的了吗?
flash的回收机制,好象是说如果没有引用指向该对象,那么系统会自动回收.
cs~ cs~ cs~暴头,暴头........
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.