aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Hi,
After going through some of the threads related to this topic and some mocks, I still could not really grab a full understanding on it, especially on which objects are eligible for gc or not.
I understand that string literals and primitives are not eligible for gc. But how do I know which objects are eligible for gc, once they are set to null.

Clement


best regards,<br />Steven<br />SCJP, SCEA
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Draw a picture. Put each variable on the page and each object and then draw an arrow from each variable to the object that it references, like this:

Then, when more code is executed, just modify your picture:

Notice that, in the second picture, there are no more arrows pointing to object "a". Once that happens, that object it eligible for garbage collection. This always helps me in questions of this type.
You can also take a look at this Flash App which, although not focused on garbage collection, does illustrate this concept.
I hope that helps,
Corey


SCJP Tipline, etc.
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
OK, I'll try out the formula.
Thanks for the great explanation, Corey.
Clement
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
That's a great explanation Corey!
I like pictures!


Rob
SCJP 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Ahh, yes - the joys of learning C++ and pointers. If you ever had to learn pointers, you'll never have problems with references in Java.
Corey
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Hi,
A question:
At what stage in the following method does the object initially referenced by s
becomes available for garbage collection. Select the one correct answer.

Why is the answer d and not c? How would I apply ur formula to this type of question, Corey?

Thanks,
Clement
Rodney Teggins
Greenhorn

Joined: May 06, 2002
Posts: 10
Clement
Until //3 has been executed the reference "s" is still pointing at the original object so can't be before this statement. At //4 "s" now points at a new object and the unreferenced object is now eligable. (At the end of //3..)
Rodney
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Clement Ng:
How would I apply ur formula to this type of question, Corey?

Okay, the joys of ASCII art....here goes...
I'll draw the picture after each line of code is executed. In general, I just draw a picture (in pencil) and erase and redraw references as I need to. So, you can imagine that each of these successive pictures is really the same picture going through a progression. Anyway, here goes:

We have a new object created which r references.

Now we have two reference variables, r and s, that reference two different objects. I've marked the object referenced by s with a '*' to indicate that this is the one we're watching to see when it is garbage collected.

When the 1 is concatenated to the end of teh object referenced by r, a new object is created and r is assigned a reference to it. Therefore, the top object in the above picture is now eligible for garbage collection.

By assigning null to r, the second object is also now available for garbage collection.

After this line of code is executed, another new object is created (due to the concatenation) and the variable s is assigned a reference to it. Notice that the object labelled with a '*' now has no references to it, so it is now available for garbage collection. So, the answer is 'd', the object originally referred to by s is available for garbage collection after this line is executed.
I hope that helps,
Corey
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
If you want to see a more animated version of this, take a look at this Flash app. It was made to cover passing parameters, but there is some discussion of garbage collection and you can see what my drawings look like in an animated fashion.
Corey
Rasri Anand
Greenhorn

Joined: Apr 19, 2002
Posts: 16
This is from the boone exam Q.13:

At which point is the object referred to by the variable "first", eligible for garbage collection?
The answer given is:
After the line labelled d: has executed.

I thought since args[0] would still be referring to the object initially referred to by first, it cannot be eligible for garbage collection till args[0] = null has been executed.
Can somebody please explain this?
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Thanks a lot for the great explanation, Corey.

Clement
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Ok, I'm gonna take it a shot using Corey's way to explain your answer. Someone correct me if I'm wrong.
1. Here, a new String object is created which first references.


2. Another new String object is created which second references.

3. Another new String object is created which riddle references.

At the next line, first references to a null now , leaving the String object originially pointed to being eligible for garbage collection.

Thus, the object referred to by the variable "first", is eligible for garbage collection after the line labelled d: has executed.

Clement
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
That is correct. Nice work. If you can draw pictures like this, you won't have any problems with these sorts of questions.
Corey
Rasri Anand
Greenhorn

Joined: Apr 19, 2002
Posts: 16
Thanks for the explanation Clement. It helped.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If line a had been: first = args[0];
then Rasri would have been correct.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
Hi folks,
Corey - your explanations are really great. Do you have an editor for your ASCII art - if yes, which one, where can I get a copy of it?
One question: What about the pool for string literals?
I mean, does anybody know how GC treats string literals? I'd expect they cannot be gc-ed before the object that keeps the string literal in its pool is eligible for GC.
I'd be grateful for any comments!
[ May 10, 2002: Message edited by: Mag Hoehme ]

Mag
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Mag Hoehme:
Corey - your explanations are really great. Do you have an editor for your ASCII art - if yes, which one, where can I get a copy of it?

Nope - this is just plain old text written within code tags. I don't really use ASCII when I'm trying to answer questions on an exam - this is just the only way I know of showing roughly what my pencil and paper sketches look like without scanning them onto a computer.

One question: What about the pool for string literals?
I mean, does anybody know how GC treats string literals? I'd expect they cannot be gc-ed before the object that keeps the string literal in its pool is eligible for GC.

You should check out this thread for a good discussion of how string literals are handed in Java. In short, when you use a constant (such as a String literal), a reference is made to that object in a String constant table. This reference never goes away (until the JVM dies), so the literals are never eligible for garbage collection. You see, those objects still adhere to the normal rule of garbage collection: "Once there are no references to an object from an active part of the program, it is eligible for garbage collection." However, as this reference from the constant table never goes "out of scope," there is always a reference to a String literal.
Check out that thread. Rob Ross does an excellent job of explaining this.
Corey
 
 
subject: Garbage Collection
 
Similar Threads
Arrays and GC
garbage collection
Garbage Collection
GC problem
MindQ question 36 garbage collection