flex内存泄露解决之道

(1)Event Listeners
                Listening to parent objects does cause memory leaks

                e.g.

                    override protected function mouseDownHandler(…):void {
                     systemManager.addEventListener(“mouseUp”, mouseUpHandler);

              you can:

                1.Removing the addEventListener (when dispose).

                  systemManager.removeEventListener(“mouseUp”, mouseUpHandler);

                2. Use of weak reference listeners

                  override protected function mouseDownHandler(…):void {
                  systemManager.addEventListener(“mouseUp”, mouseUpHandler, false, 0, true);

              These do not block garbage collection(generally do not cause memory leaks):

Weak References
Self References
References to Child Objects
                    weak reference event listener  e.g.

                      someObject.addEventListener(MouseClick.CLICK, handlerFunction, false, 0, true);

                   Self References  e.g.

                       this.addEventListener(MouseClick.CLICK, handlerFunction);

                   childObject event listener e.g.

        private var childObject:UIComponent = new UIComponent;
        addChild(childObject);
        childObject.addEventListener(MouseEvent.CLICK, clickHandler);

           建议对所有addEventListener都要removeEventListener,或是使用Weak References .



         Reference :
          http://blogs.adobe.com/aharui/2007/03/garbage_collection_and_memory.html

           http://www.dreamingwell.com/articles/archives/2008/05/understanding_m.php   

       

(2)   static members
           e.g.

          Class (或MXML)中有:

           public static var _eventService : MyService=new MyService();

          在dispose时,需要设置:

          _eventService =null;

       

(3)module (未解决)
          moduleLoader unloadModule后

         ModuleInfo 并不会被GC.

         Garbage Collection in a MultiCore Modular Pipes Application

        这篇文章介绍了一种GC策略,感觉对于ModuleInfo 的GC无效。



(4)CSS Style
          module 中如果使用了shell的CSS定义或是<mx:Style> 这样的定义,那么这个module将不能GC.

          弹出的窗口应该是同样的结果.

         解决方法,使用动态CSS文件

       module   init中

         StyleManager.loadStyleDeclarations("css/myStyle.swf");

      module  dispose中

         StyleManager.unloadStyleDeclarations("css/myStyle.swf"); 



(5)TextInput/Textarea(未解决)
          如果module中有window使用了TextInput/Textarea控件,不点击没有问题,只要点上去,那么很遗憾了,module和所在窗体将不能被GC.

         这个BUG非常严重,目前还没有解决方法。

         memory leak when using TextInput and TextArea when click the keyboard这里面附加的解决方法无效。

        通过profiler分析,应该和Focusmanager有关,只有一点击就不会释放。



(6)CursorManager.setCursor
        使用了

          cursorID = CursorManager.setCursor(iconClosed);

         dispose时要

           CursorManager.removeCursor(cursorID);



(7)Bitmap
           如果使用Bitmap,结束时需要调用其dispose方法,否则内存消耗巨大。

var bmp:Bitmap  =new Bitmap();

........

if (bmp.bitmapData!=null) {
bmp.bitmapData.dispose();
}



(8) 其他
          binding也疑似有memor leak 问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值