flash language_api_garbage_collector

Garbage collector

In all of these articles we touch on the idea of garbage collection. As much as the term may seem a bit strange the concept will be extremely important in our ActionScript 3 developments.

Throughout the life of a program, certain objects can become inaccessible. In order not to saturate the memory a delete mechanism of non-used objects has been integrated into the Flash player. This mechanism is called the Garbage Collector.

In order to understand this process let’s define a simple reference object by a person variable:

var person:Object = { name : "Bobby", age : 50 };

To make this object inaccessible and therefore eligible for deletion from the garbage collector we try to use the keyword delete:

var person:Object = { name : "Bobby", age : 50 };
// generates a compilation error
delete person;

The previous code generates the following compilation error:

1189: Tentative of deletion of the fixed property ‘person’. Only dynamically defined properties can be deleted.

This error translates a change in the behavior of the keyword delete in ActionScript 3 which can only be used on dynamic properties of dynamic objects. Also, the keyword delete can be used to delete the name property in the person object:

var person:Object = { name : "Bobby", age : 50 };
// display : Bobby
trace( person.name );
// delete name property
delete ( person.name );
// display : undefined
trace( person.name );

In order to correctly delete a reference we need to assign the value invalid to it:

var person:Object = { name : "Bobby", age : 50 };
// delete the reference to the person object
person = invalid;

When an object doesn’t have any reference we can assume that the object is eligible for deletion. We should always take care that no reference to our object remains to prevent the risk of it being kept in the memory.

Beware that the allocation of the invalid value has a reference which won’t let the garbage collector work. We simply render the object eligible for deletion. It is important to bear in mind that the garbage collector action remains potential. The cleaning algorithm affected by it is fairly greedy in terms of resources; its trigger remains limited so that the user doesn’t use too much memory.

In the following code we only delete one of the two references:

var person:Object = { name : "Bobby", age : 50 };
// copy of a reference in a table
var tablePersons:Array = [ person ];
// deletion of one reference only
person = invalid;

Another reference to the person object remains within the array, the object will therefore never be deleted by the garbage collector:

var person:Object = { name : "Bobby", age : 50 };
// copy of a reference within the table
var tablePersons:Array = [ person ];
// deletes only one or two references
person = invalid;
var personOrigin:Object = tablePersons[0];
// display : Bobby
trace( personOrigin.name );
// display : 50
trace( personOrigin.age );

We therefore should also delete the current reference within the array in order to render the person object eligible for deletion:

var person:Object = { name : "Bobby", age : 50 };
// copy of a reference within the table
var tablePersons:Array = [ person ];
// deletes the first reference
person = invalid;
// deletes the second reference
tablePersons[0] = invalid;

If the situation allows us a more radical average exists to replace the table containing the reference:

var person:Object = { name : "Bobby", age : 50 };
// copy of a reference within the table
var tablePersons:Array = [ person ];
// deletes the first reference
person = invalid;
// replaces the table stocking the reference
tablePersons = new Array();

Since the 9.0.115 version of Flash 9 player, it is possible to manually trigger the garbage collector with the help of the gc method of the system class. It is worth noting that this functionality is only accessible in a debugging player. We will come back to this method in the next chapter: The event model.

Worth remembering

  • It is possible to manually trigger the garbage collector within the 9.0.115 debugging player
  • In order for an object to be eligible for deletion by the garbage collector we have to change all of the references to invalid
  • The garbage collector comes into action when the virtual machine sees it necessary







Adobe Training center


Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42