Flash Lite 會定期從記憶體清除檔案不再參照的任何物件和變數。 這稱為記憶體回收。 Flash Lite 會每隔 60 秒或是在檔案記憶體使用量突然增加 20% 或以上時,執行記憶體回收程序。
雖然無法控制 Flash Lite 執行記憶體回收的方法和時機,您仍然可以刻意釋放不需要的記憶體。 對於時間軸或全域變數,使用 delete 陳述式以釋放 ActionScript 物件所使用的記憶體。 對於區域變數 (例如,在函數定義中定義的變數),無法使用 delete 陳述式來釋放物件的記憶體,但可以將參照物件的變數設定為 null。 只要該物件沒有其他參照,這樣就能釋放物件所使用的記憶體。
下列兩個程式碼範例顯示如何刪除參照物件的變數,以釋放這些物件所使用的記憶體。 這些兩個範例完全相同,除了第一個範例是建立時間軸變數,而第二個範例則是建立全域變數。
// First case: variable attached to a movie or // movie clip timeline // // Create the Date object. var mcDateObject = new Date(); // Returns the current date as a string. trace(mcDateObject); // Delete the object. delete mcDateObject; // Returns undefined. trace(mcDateObject); // // Second case: global variable attached to a movie or // movie clip timeline // // Create the Date object. _global.gDateObject = new Date(); // Returns the current date as a string. trace(_global.gDateObject); // Delete the object. delete _global.gDateObject; // Returns undefined. trace(_global.gDateObject);
如前所述,您無法使用 delete 陳述式來釋放區域函數變數所使用的記憶體, 而要將變數參照設定為 null,這與使用 delete 的效果相同。
function func()
{
// Create the Date object.
var funcDateObject = new Date();
// Returns the current date as a string.
trace(funcDateObject);
// Delete has no effect.
delete funcDateObject;
// Still returns the current date.
trace(funcDateObject);
// Set the object reference to null.
funcDateObject = null;
// Returns null.
trace(funcDateObject);
}
// Call func() function.
func();
如需針對行動電話和裝置建立內容的更多秘訣和技巧,請參閱 www.adobe.com/go/learn_cs_mobilewiki_en。