User Guide

78 Optimizing Content for Performance and Memory
Although you cannot control how and when Flash Lite performs garbage collection, you can
still free unneeded memory deliberately. For a timeline or global variables, you can 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 cant use the
delete statement
to free an objects 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 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
var mcDateObject = new Date(); // Creates the Date object.
trace(mcDateObject); // Returns the current date as a string.
delete mcDateObject; // Deletes the object.
trace(mcDateObject); // Returns undefined.
// Second case: global variable attached to a movie or
// movie clip timeline
_global.gDateObject = new Date(); // Creates the Date object.
trace(_global.gDateObject); // Returns the current date as a string.
delete _global.gDateObject; // Deletes the object.
trace(_global.gDateObject); // Returns undefined.
As mentioned previously, you cant use the delete statement to free memory that a local
function variable uses. Instead, you can set the variable reference to
null, which has the same
effect as using
delete. The following code example demonstrates this.
function func(){
var funcDateObject = new Date(); // Creates the Date object.
trace(funcDateObject); // Returns the current date as a string.
delete funcDateObject; // No effect.
trace(funcDateObject); // Still returns the current date.
funcDateObject = null; // Sets object reference to null
trace(funcDateObject); // Returns null.
}
func(); // Calls func() function.
About loading data and memory limits
When developing applications for mobile devices, its best to minimize the amount of data
you attempt to load at one time. If you are loading external data into a Flash Lite application
(for example, using
XML.load()), the devices operating system may generate a “memory
failure” error if a big enough piece of memory is not allocated to store the incoming data. This
situation can occur even if the total amount of remaining memory is sufficient.