PDA

View Full Version : A question about GC Mark and Sweep


Assertnfailure
10-09-2006, 04:58 PM
In one of seno's "AS 3.0 tips of the day", there was a topic about garbage collection, and how a situation such as this:
var a:Object = new Object(); // reference(a) count 1
var b:Object = new Object(); // reference(b) count 1
a.b = b; // reference(b) count 2
b.a = a; // reference(a) count 2
delete a; // reference(a) count 1
delete b; // reference(b) count 1

Requires mark and sweep.

Does this imply that mark and sweep is necessary for cross-references between objects that are deleted, or for recursive references within objects that are deleted?

In other words, does this also require mark and sweep:
var a:Object = new Object();
a.b = new Object();
delete a;
to delete a.b's object reference?

Assertnfailure
10-14-2006, 10:01 PM
I find it rather frustrating that most of my questions go unanswered...no matter how short the answer may be (like in this case nothing more than a "yes" or "no")

Meanwhile someone asks what a png is and gets 7 replies.

This post will probably be overlooked as well, which kinda makes me not even want to contribute to this forum :mad:

senocular
10-14-2006, 10:18 PM
The thing is, a heck of a lot more people know what a PNG is compared to those who know about ActionScript 3 garbage collection. Chances are, your other questions are probably out of the range of knowledge of a lot of people here, too, which is why they might not get answered. And when you have people of your knowledge (higher than average) feeling the same way as you in your third post, you can see why there may even be less people to answer your question.

In fact, I don't even know the answer to this question. I know its necessary when references exist for objects which are not in some way connected to root, but I am not sure what happens to the b reference in a when its collected. Will the b reference be deleted when a is collected thereby settings its reference count to 0 allowing it to collected too? I honestly don't know. You might be able to test it with some trials and checking using System.totalMemory.

jsebrech
10-14-2006, 10:55 PM
Assertnfailure, don't sweat it. Most of my questions didn't get answered either. I figure it's because they're good questions. Well, that or they're really dumb :) I stick around here mostly to help other people get ahead with actionscript, and to see what cool tricks the "wise hands" here offer up from time to time.

For the garbage collection questions I'd try to mail one of the flash developers. Perhaps you could contact the penguin.swf guy.

Assertnfailure
10-15-2006, 12:08 AM
Oh ok...I didn't realize that question was as complicated as it was. I wasn't even sure if people bothered to read it at all...

Where WOULD be the best place to ask such questions? o.O

dr_zeus
10-17-2006, 12:15 AM
You could try Adobe's official forums. Even if no one there can answer your question, someone might be able to pass it on to one of the Flash Player engineers.

csdstudio
10-17-2006, 08:37 PM
...
In fact, I don't even know the answer to this question.

Well if senoc doesn't know, we're all screwed. :D

csdstudio
10-17-2006, 08:38 PM
I find it rather frustrating that most of my questions go unanswered...no matter how short the answer may be (like in this case nothing more than a "yes" or "no")

Meanwhile someone asks what a png is and gets 7 replies.

This post will probably be overlooked as well, which kinda makes me not even want to contribute to this forum :mad:

That's kinda lame. Since when does an answer's level of complexity have any relation to its length of words?

MichaelxxOA
10-17-2006, 09:24 PM
I'm not quite sure what you mean by, is mark and sweep necessary, but...

When you use the delete operator it doesn't free memory, the delete operator instead unbinds the variable from the location in memory the variable references. Marking and sweeping is done internally by the GC, which means that all reference management is also done internally.

If you create an object and then delete it, you'll see that the total memory the application is using does not change. It's important to note however that once the last reference to an object is unbound the object cannot be accessed. (through Actionscript)

For example:

trace( System.totalMemory ); // some value
var arr:Array = new Array();
arr.push( new Object() );
trace( System.totalMemory ); // value bigger than some value
delete arr[0];
trace( System.totalMemory ); // same as last trace


Note here that the values that are traced will be different depending on where and how you are testing this.

Since you can see that everything is done internally, it's going to be easy to visualize the answer to your question.

--


var a:Object = new Object();


Here we have an object, with one single reference (a).

If we create an object as a property (b) of object a, we get the following.


var a:Object = new Object();
a.b = new Object();


Now we have two objects, both with a single reference. The newly created objects reference is a property of object a.

When we delete object a we lose it, because it has no more references.

deleting object a

var a:Object = new Object();
a.b = new Object();

delete a;


When the garbage collector makes a pass over, it has no way to access object a, and because it cannot access a it has no way to see that it is referencing our second object b. The GC then makes the decision that there are no references to the second object left, and moves it so that memory can be recycled.

The short answer I guess being yes, mark and sweep is still required, but no you do not have to delete that reference yourself.

Michael

When does mark and sweep take place?

The mark and sweep model implemented by Adobe is incremental, meaning that collection cycles are done randomly (or on an interval) throughout the life of the application.

dvlnblk
10-18-2006, 02:25 AM
I find it rather frustrating that most of my questions go unanswered...no matter how short the answer may be (like in this case nothing more than a "yes" or "no")

Meanwhile someone asks what a png is and gets 7 replies.

This post will probably be overlooked as well, which kinda makes me not even want to contribute to this forum :mad:


LOL, I feel your pain AF, most of my questions go unanswered too. But once in a while you get lucky and someone does. Way to go Mike...Dvl