• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
majohnad majohnad
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nick Shrine
Greenhorn
Posts: 11
Hibernate Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, Bert.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
superb!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic