File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GARBAGE COLLECTION (HFJ PG 266 ) 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 (HFJ PG 266 )" Watch "GARBAGE COLLECTION (HFJ PG 266 )" New topic


srikanth mycherla

Joined: Oct 31, 2008
Posts: 26
Q)/*Which of the lines of code if added to the class on the left at point A . would cause exactly one additional object to be eligible for the garbage collector ? ( Assume that point A will execute for a long time giving the garbage collector time to do its stuff )*/

These can be substituted in the place of A
1.copyGC = null;
3.newGC =gc3;

These are my assumptions

A) Starting with main method line 7 indicates that we are creating one reference variable for the class GC
B) line 8 indicates that the reference variable gc2 is pointing to one GC object
C) line 9 indicates that the reference variable gc3 is pointing to another new Gc Object

D) line 10 indicates that we are copying ref variable gc3 into gc4 that indicates gc4 and gc3 are pointing to same object

E)line 11 gc1=doStuff(); indicates the compiler to search for doStuff() method ..the compiler sees that the gc1 ref variable type is GC so it goes to line 2 . there it sees -> public static GC doStuff(). here it sees static method doStuff whose return type is GC. and it sees the type GC and checks that the calling type is also GC so it proceeds further ..and goes to line 3 GC newGC = new GC (); which indicates that another reference variable newGC of type GC is pointing to another new object GC ();

F) THEN IT GOES to line 4 doStuff2(newGC); and enters into line 12
where local variable copyGC holds the reference variable newGC

BUT THE OBJECT WHICH WAS CREATED IN CLASS GC doesn't die due to passing of reference variable into copyGC . (AM I RIGHT )
G) Again it comes back to line 4 and goes into line 5 return newGC; and newGC IS RETURNED TO LINE 11.gc1=doStuff(); SO NOW AGAIN THE COMPILER SEES THE TYPE OF newGC which is GC AND TYPE OF gc1 WHICH IS GC so newGC is assigned to gc1.

H) the moment the value is returned to line 11 the object newGC() in static GC doStuff() DIES as the scope is out of the method

I) now gc1=newGC means both are reference variables of class GC but pointing to no object ( AM I RIGHT HERE ) what it indicates by saying gc1= newGC . IN MY VIEW
SINCE NEWGC'S OBJECT is died . By saying gc1=newGC it indicates gc1 = null or can we access gc1 and newGC IN MAIN??? WHICH IS CORRECT ?

J) now substituing the values in the place of A

K) .copyGC = null;// ATTEMPTS TO ACCESS A VARIABLE WHICH is out of scope (not permitted )

L).gc2=null; //permitted since gc2 holds only one object and if we assing it to null the object is eligible for GC so ok

M).newGC = GC3; // OUT OF SCOPE

N) [b]GC1=NULL// IAM CONFUSED WITH THIS .i think gc1 is not pointing to any object then whats the use of saying GC1=null ;[/b]

I THINK since GC1=null another reference variable newGC is also pointing to GC1 so newGC also holds null

but this line is permitted according to answer but why when gc1 and newGC reference variables are not pointing to any object how can we say that the object is eligible for GC (DOUBT)

O)newgc=null// out of scope ok

P).gc4=null; // no since we know gc3 and gc4 are pointing to same object . by assigning gc4 to null gc3 is still pointing to object so not eligible for garbage collection

Q). gc3=gc2;// no since gc4 is still referring to the object
(no doubt) here also

R).gc1=gc4; // here the answer says ok but i didnt understand why

here gc4 is pointing to the object which is pointed by gc3 and when we say gc1=gc4 then gc1 also points to the same object

so gc1 gc4 and gc3 are pointing to same object am i right?

till now gc1 is not holding any object when we say gc1 = gc4 then how come the object is eligible for garbage collection according to answer

S)gc3= null // ok sunce gc4 is still referring to the object

i have written A to S statements which is my opinion towards the above code
please correct me where my way of thinking is wrong by seeing the A TO S statements .

Please indicate when the object is eligible for GARBAGE-COLLECTION between function calls ?
please correct me ranchers

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Its too long Srikanth, no body has so much time and courage to read it and answer it, please make it short and ask question in short terms.

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
yes srikanth...
your assumptios are right.onething N for which i am also thinking
there is only one object which is eligible for garbage collection if we place gc2=null in line-A..

not only one answer it has another answers also....

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
Hi srikanth
if you can post which contains lot of code or doubts then it is difficult for our ranchers to clarify your doubts so try to post it short and more clear..
anyway as per my knowledge...i am stating that

yes object wont die it is referenced by copygc and newgc

here newgc is local variable and scope is with in the method dostuff() then how can you use that in another method?

after executing the dostuff() and dostuff2() methods object created in dostuff() method is only pointed by gc1.
so gc1=null is also one of the answer..

here gc1 is pointing to one object if it refernced to object which is referenced by gc4 then one object is elgible for GC

so the answers are

srikanth mycherla

Joined: Oct 31, 2008
Posts: 26
Thank you Ganesh for your are right
I agree. Here's the link:
It's not a secret anymore!