• 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 (HFJ PG 266 )

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
2.gc2=null;
3.newGC =gc3;
4.gc1=null;
5.newGC=null;
6.gc4=null;
7.gc3=gc2;
8.gc1=gc4;
9.gc3=null;

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

 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

FOR F:
yes object wont die it is referenced by copygc and newgc


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

FOR N:
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..

FOR R:
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

2)gc2=null
4)gc1=null
8)gc1=gc4
:roll:
 
srikanth mycherla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ganesh for your reply..you are right
 
Ranch Foreman
Posts: 872
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The last post for this thread looks as if it was 14 years ago.

What I can not teil is if the author wants us to insert one line at a time or are the test lines accumulated?

If any of the folks on this thread are around to answer please let me know.

Thanks,

Kevin
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic