Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory usage and garbage collection of Objects in AS3

I want to know about the Object type specifically when it comes to garbage collection in Flash.

I know that items will be ready for garbage collection in situations like this:

// create
var ar:Array = [];

var mc:MovieClip = new MovieClip();
mc.addEventLisntener(blah, blah);

ar.push(mc);
addChild(mc);

// kill & gc
ar.splice(0, 1);
mc.removeEventListener(blah, blah);
removeChild(mc);

But how/will an Object get garbage collected in situations like below.

Say I have a function in my class MartysMC that I parse an Object through:

package
{
    import flash.display.MovieClip;

    public class MartysMC extends MovieClip
    {
        /**
         * Updates this
         * @param obj An object containing key/value pairs to represent new property values
         */
        public function update(obj:Object):void
        {
            var i:String;
            for(i in obj)
            {
                this[i] = obj[i];
            }
        }
    }
}

And now I make use of this function like so:

var mmc:MartysMC = new MartysMC();

var dataObject:Object =
{
    x: 10,
    y: 34,
    alpha: 0.6
};

mmc.update(dataObject);

What happens to dataObject? Will this get garbage collected from here? Even still, what about the object in this line:

mmc.update({x:15,y:18,name:"marty"});
like image 802
Marty Avatar asked Jun 01 '11 07:06

Marty


People also ask

What is memory management and garbage collection?

In computer science, garbage collection (GC) is a form of automatic memory management. The garbage collector attempts to reclaim memory which was allocated by the program, but is no longer referenced; such memory is called garbage.

Does garbage collection use memory?

The garbage collector provides the following benefits: Frees developers from having to manually release memory. Allocates objects on the managed heap efficiently. Reclaims objects that are no longer being used, clears their memory, and keeps the memory available for future allocations.

How does the garbage collector determine which objects to remove from memory?

An object is considered garbage when it can no longer be reached from any pointer in the running program. The most straightforward garbage collection algorithms simply iterate over every reachable object. Any objects left over are then considered garbage.

What is garbage collection in VB net?

NET's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.


1 Answers

To see what happen with the GC you can use a Dictionary with a weak reference set to true and using the object you want to cheeck as a key:

var d:Dictionary = new Dictionary(true)
d[myObject] = whatever

when the object will not be longer available it will be delete from the dictionary.

here a complete sample based on your example at wonderfl : http://wonderfl.net/c/e9W4

you see that very quickly both of your object have been garbage collected.

like image 129
Patrick Avatar answered Sep 23 '22 23:09

Patrick