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"});
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With