Janyou's blog

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

        从目前情况看,Flex 3(AS3)存在严重的memoy leak(内存泄露)问题,这些问题中一部分可以合适的编码方式来避免,还有一些问题目前只有等待Flex SDK更新了。

感觉Flex 的商业应用目前只能在初级阶段。

      列举一些产生memoy leak的情景。

  (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):

    1. Weak References
    2. Self References
    3. 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 问题。

         ......

         感觉Flex/AS 3离商业开发还有很长的路要走。

posted on 2008-11-25 16:37  janyou  阅读(2357)  评论(0编辑  收藏  举报