DeviceCentral

TOPIC APPEARS IN:

Managing Flash Lite file memory for mobile devices

Flash Lite regularly clears from memory any objects and variables that a file no longer references. This is known as garbage collection. Flash Lite runs its garbage-collection process once every 60 seconds, or whenever usage of file memory increases suddenly by 20% or more.

Although you cannot control how and when Flash Lite performs garbage collection, you can still free unneeded memory deliberately. For timeline or global variables, use the delete statement to free the memory that ActionScript objects use. For local variables—for example, a variable defined within a function definition—you can’t use the delete statement to free an object’s memory, but you can set to null the variable that references the object. This frees the memory that the object uses, provided there are no other references to that object.

The following two code examples show how to free memory that objects use by deleting the variable that references those objects. The examples are identical, except that the first example creates a timeline variable and the second creates a global variable.

// 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);

As mentioned previously, you can’t use the delete statement to free memory that a local function variable uses. Instead, set the variable reference to null, which has the same effect as using 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();