• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection

 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I'll try out the formula.
Thanks for the great explanation, Corey.
Clement
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a great explanation Corey!
I like pictures!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the great explanation, Corey.

Clement
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation Clement. It helped.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If line a had been: first = args[0];
then Rasri would have been correct.
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic