2010年4月2日 星期五

Garbage Collection


在Flash內GC的工作是Player本身會自動會去執行,但在程式開發時要
去儘可能回收不必要的記憶體暫存,降低記憶體使用量
1.對Loader物件
在使用Loader物件載入外部圖檔或swf,當已經不再需要使用時
可以loader.unloadAndStop(true),會強制卸載載入物件並做GC
2.對BitmapData
當BitmapData不再使用,要釋放bitmapData內存的資料
可以用bitmapData.dispose();
3.一般DisplayObjectContainer
除了對不必要的child做remove
DisplayObjectContainer.removeChildAt(0);
若是屬AIR專案
可以執行System.gc()強制立刻執行GC
4.判別Child的Class 類型
當要移除Child時,要知道Child的Class類型才可以使用適當的方式處理


var sourceName:String = getQualifiedClassName(_source.getChildAt(0));
var sourceType:Class = getDefinitionByName( sourceName) as Class;
if (sourceType == FlexLoader) {
    _source.unloadAndStop(true);   
}else if (sourceType == Bitmap) {
    Bitmap(_source.getChildAt(0)).bitmapData.dispose(); 
}
_source.removeChildAt(0);
System.gc();

沒有留言: