Flash Builder Profiler——修复ExternalInterface上的内存泄露
即时获得麻球游戏开发的最新消息,关注游戏开发者热门讨论,请各位开发者申请加入麻球官方群:121304476
也可以follow catfly围脖:t.sina.com.cn/iscat
  • 资讯类型: 翻译
  • 来源页面: http://www.flashmobileblog.com/2010/06/17/flash-builder-profiler-memory-leak-on-externalinterface/
  • 资讯原标题: Flash Builder Profiler – Fixing Memory Leak on ExternalInterface
  • 资讯原作者: Mark Doherty
  • Flash Builder 4 Profiler, ExternalInterface Memory leak from Mark Doherty on Vimeo.

    Flash应用最见的一个问题是内存泄露,也就是由于代码的编写上的缺点导致Flash Player失去对某段内存的控制而无法对其进行回收再用。在移动开发中非常重要的一点就是要理解内存管理从而发挥出Flash Player的最大功效,并最终给用户一个稳定的使用环境。

    Flash Player 内存管理
    Flash Player使用自动内存管理来方便你的开发过程并减少代码量。事实上Flash Player使用了一个非常简单的机制,这个机制会确定你引用某个特定对象的次数。一旦有一个对象的被引用次数为零,那么这个对象就会被垃圾回收器回收——准备地讲这种机制叫作“引用计数法(reference counting)”。

    下面是一个例子,注意我创建了一个Geolocation对象并添加了一个updateHandler作为update事件的侦听器函数。这样updateHandler的引用次数就增加了一个。

    
    var geo:Geolocation = new Geolocation();
    geo.addEventListener(GeolocationEvent.UPDATE, updateHandler);
    
    function updateHandler(event:GeolocationEvent):void
    {
            geo=null;
            trace(event.longitude);
    }
    

    这个updateHandler函数把geo对象设为null,标记它可以被垃圾回收器回收。但是,由于这个geo对象仍然保留有一个对updateHandler的引用,所以geo对象不能被回收。下面我们移除侦听把引用给释放掉。

    
    function updateHandler(event:GeolocationEvent):void
    {
            geo.removeEventListener(GeolocationEvent.UPDATE, updateHandler);
            geo=null;
            trace(event.longitude);
    }
    

    使用Flash时很容易导致内存泄露,有时甚至很难调试出来。所以在开发应用的时候必须时刻小心并且在销毁对象的时候使用各种工具来检测是否存在内存泄露。

    Flash Builder Profiler
    Flash Builder附带了一个叫Profiler的新功能,在视频中我会给你们演示怎么样去使用Profiler来解决内存泄露问题。

    猫推拼盘