This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Mock Exam Errata and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Mock Exam Errata
Reply Bookmark "Garbage collection " Watch "Garbage collection " New topic
Author

Garbage collection

H Singh
Ranch Hand

Joined: Apr 03, 2005
Posts: 34
At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.


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



Before statement labeled 1
Before statement labeled 2
Before statement labeled 3
Before statement labeled 4
Never.

Can someone explain what will be the answer to this question and why ?
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
My Answer is "Never".
To be more correct it's eligible for garbage collection after "methodX()" get's executed.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Sisir Chanda
Greenhorn

Joined: Jan 03, 2005
Posts: 11
yeahh..thats correct...because GC checkes whether the reference is being used anymore or not before doing garbage collection...since before line 4 the reference of s is being used so it will not be collected before the method returns.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
I disagree. The correct answer is "Before statement labeled 4". The reason is that "the object initially referenced by s" is a String object with the value of "abc". In statement 3, a String object is created with the value "abcnull" and that String is then assigned to s. As a result, the "abc" String that used to be referenced by s is no longer referenced by anything, so the object initially referenced by s is eligible for garbage collection as soon as statement 3 completes, which is immediately before statement 4.


SCJA 1.0 (98%), SCJP 1.4 (98%)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
Originally posted by Sisir Chanda:
yeahh..thats correct...because GC checkes whether the reference is being used anymore or not before doing garbage collection...since before line 4 the reference of s is being used so it will not be collected before the method returns.


where u have read this about...
give me the linke....

i think joe sanawitz is perfectly right and i have seen that explanation every where but your statement is to be honest strange..

anyway give me the link from where you have read this..


Thanks and Regards,<br />Amit Taneja
Sisir Chanda
Greenhorn

Joined: Jan 03, 2005
Posts: 11
yep...It was my mistake...I read that the equestion is '...when the object refernced by s is eligible for GC...'. Since before line 4 , '....s = s + r; //3
...' was used, so my answer was s will never be eligible for GC. But the actual question was '....when the INITIAL object referenced by s will be eligible for GC.....'

The the answer is definitely....Before statement labeled 4. since at line 3 s has been assigned to a different object and s is referring to a different object. Having said that, the INITIAL object reference by s will become eligible for GC.

Thanks guys!!!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Garbage collection
 
Similar Threads
doubt on garbage collection
Garbage collection
q on garbage collection
GC doubt
Garbage Collection