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

majohnad majohnad
Greenhorn

Joined: Jan 01, 2003
Posts: 24
Hi,
void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4

In the above code in which line s is available for garbage collection.
Thnaks in Advance


zxjohn
Nick Shrine
Greenhorn

Joined: Jul 17, 2003
Posts: 11
Originally posted by majohnad majohnad:
Hi,
void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4

In the above code in which line s is available for garbage collection.
Thnaks in Advance

Unless I'm mistaken, I'd say line 3.
s initially contains a reference to the String object created by String s = new String("abc").
I always draw a diagram like this:
r ------------> (1)
s ------------> (2)
incrementing the number on the right (the heap) every time a new object is created and showing which reference variables point to which objects, then it is easy to see when an object is eligible for GC when it has no refs pointing to it. So in this case after line 3 we have:
(1)
(2)
(3)
r ------------> null
s ------------> (4)
So we can see that 3 objects are eligible for GC. (3) is the object created at line 1 and (4) is the new String object resulting from the addition at line 3.
Also, I presume your question should read:
"in which line is the object initially assigned to s eligible for GC". s itself is never eligible for GC as it is a stack variable and therefore not on the garbage collectible heap.
(I assume I am right in thinking that when you add Strings the new String does not contain references to the Strings that were added but merely constructs a new String object out of the data contained in the two added Strings.)


Nick<br />SCJP<br />SCJD
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Just to make sure we're all on the same page... I think I agree with Nick, but I can't quite follow the references.
Let's re-label the code, to be more like what's on the real exam:
Also let's not use String objects because of the confusion about the String pool. The exam won't mix GC questions together with String questions!


I like Nick's pictures
After line 3 there are two reference vars. and two objects:
Stack ......... Heap
r --------- > (SB1) // StringBuffer object 1
s --------- > (SB2)
After line four:
................... (SB1) // has no ref. var
s ----------> (SB2)
r ----------> (SB3) // new object, old ref var
After line 5:
................... (SB1) // no ref var
s ------------> (SB2)
.................... (SB3) // no ref var
r ------------> null
After line 6, no changes in ref vars because s is refering to a StringBuffer object.
After line 7 the method is popped off the stack and the reference to SB2 is also lost.
So in this example, depending on the line #, the number of objects eligible for GC is the number of objects with no reference vars!
Does that make sense?
-Bert
[ July 24, 2003: Message edited by: Bert Bates ]

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you very much, Bert.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
superb!


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection