What is the garbage collection in actionscript 3.0?

[FONT=Calibri][SIZE=3]Could someone help me to understand what garbage collection is?[/SIZE][/FONT]

[FONT=Calibri][SIZE=3]Does it mean that any object needs to be deleted after its being used? If yes, how can this be done? I heve heard about using (Null) or something like that.[/SIZE][/FONT]

[FONT=Calibri][SIZE=3]Thanks,[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]fs_tigre[/SIZE][/FONT]

I’m no expert, but in loose terms;

The garbage collector is a process that continually runs in the background and removes things from memory that aren’t used.

For instance if you have a variable like thus;

var m:String = “I am such a cool string, all the other strings envy me”;

Then that variable will stay in memory even if it’s never used.

The garbage collector essentially removes things that are null and have no active listeners attached to it. It’s actionscript 3’s way of memory management. Not flawless by any means :slight_smile:

can read more here;


Garbage collection is a method of reference tracing. When an object has no references pointing it then it’s deallocated.

var foo:Object = new Object();
foo = null;//the new Object will be targeted for deletion when the garbage collector runs.

Same is true if the references pointing to that object go out of scope.

In contrast if it helps in languages like C++ which have no garbage collector then doing something like:
Object* foo = new Object();
foo = NULL;//Memory leak because now there is no way to delete the object.

Garbage collection generally takes away a lot of the small semantics so you can worry about the code.

and you have to be really careful when referencing your objects. even just one forgotten reference could be a problem when dealing with CPU intensive movies… :-/

So I don’t need to null the unused objects manually? Will flash do that automatically?

Or it is a good practice to do this manually.

var myObject:Object = Object();
myObect = null

thanks,
fs_tigre

You will need to null. Read the links sekasi posted. They’re the best explanation you’re going to get :slight_smile:

edit: actually use this one (its linked to by sekasi’s first link)

Note there are 3 parts listed on the right of the content in that link

… also Skinner’s use of delete in those examples in the first page is a little deceptive. Delete only works for dynamic properties. For class properties (anything defined with “var”) set to null.

Thank you to all.