# What is the garbage collection in actionscript 3.0?

**URL:** <https://forum.kirupa.com/t/what-is-the-garbage-collection-in-actionscript-3-0/265701>\
**Category:** flash\
**Created:** [July 11, 2008, 2:02pm UTC](https://forum.kirupa.com/t/what-is-the-garbage-collection-in-actionscript-3-0/265701 "2008-07-11T14:02:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fs\_tigre](https://avatars.discourse-cdn.com/v4/letter/f/edb3f5/32.png) [@fs\_tigre](https://forum.kirupa.com/u/fs_tigre)\
**Post date:** [July 11, 2008, 2:02pm UTC](https://forum.kirupa.com/t/what-is-the-garbage-collection-in-actionscript-3-0/265701/1 "2008-07-11T14:02:47Z")

</div>

[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]

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 11, 2008, 2:09pm UTC](https://forum.kirupa.com/t/what-is-the-garbage-collection-in-actionscript-3-0/265701/2 "2008-07-11T14:09:06Z")

</div>

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 🙂

can read more here;

> **[Failure to Unload: Flash Player 9's Dirty Secret - gskinner blog](http://blog.gskinner.com/archives/2008/04/failure_to_unlo.html)**
>
> It is nearly impossible to make Flash Player 9 properly unload ActionScript 3 content... In this article, I'll take an in-depth look at the issue, it's implications, suggestions for addressing the problem in the player, and some workarounds for the...

  

> **[AS3: Weakly Referenced Listeners - gskinner blog](http://blog.gskinner.com/archives/2006/07/as3_weakly_refe.html)**
>
> I would very strongly recommend getting in the habit of ALWAYS setting your listeners to be weakly referenced. It's easy to use weakly referenced listeners in AS3... So please (PLEASE!), get in the habit of always setting your event listeners to be...

---

<div class="post-metadata">

**Author:** ![Sirisian](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@Sirisian](https://forum.kirupa.com/u/Sirisian)\
**Post date:** [July 11, 2008, 2:23pm UTC](https://forum.kirupa.com/t/what-is-the-garbage-collection-in-actionscript-3-0/265701/3 "2008-07-11T14:23:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![johnlouis](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/johnlouis/32/568_2.png) [@johnlouis](https://forum.kirupa.com/u/johnlouis)\
**Post date:** [July 11, 2008, 2:45pm UTC](https://forum.kirupa.com/t/what-is-the-garbage-collection-in-actionscript-3-0/265701/4 "2008-07-11T14:45:25Z")

</div>

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… :-/

---

<div class="post-metadata">

**Author:** ![fs\_tigre](https://avatars.discourse-cdn.com/v4/letter/f/edb3f5/32.png) [@fs\_tigre](https://forum.kirupa.com/u/fs_tigre)\
**Post date:** [July 11, 2008, 2:49pm UTC](https://forum.kirupa.com/t/what-is-the-garbage-collection-in-actionscript-3-0/265701/5 "2008-07-11T14:49:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 11, 2008, 2:53pm UTC](https://forum.kirupa.com/t/what-is-the-garbage-collection-in-actionscript-3-0/265701/6 "2008-07-11T14:53:41Z")

</div>

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

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

> **[AS3: Resource Management pt 1 - gskinner blog](http://blog.gskinner.com/archives/2006/06/as3_resource_ma.html)**
>
> With great power comes great responsibility, and this will be very true for AS3. I will look at the underlying mechanics of the Garbage Collector, discuss the problems you are likely to face, examine the new tools available to you for handling...

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.

---

<div class="post-metadata">

**Author:** ![fs\_tigre](https://avatars.discourse-cdn.com/v4/letter/f/edb3f5/32.png) [@fs\_tigre](https://forum.kirupa.com/u/fs_tigre)\
**Post date:** [July 11, 2008, 3:29pm UTC](https://forum.kirupa.com/t/what-is-the-garbage-collection-in-actionscript-3-0/265701/7 "2008-07-11T15:29:53Z")

</div>

Thank you to all.
