aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection question 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 question " Watch "Garbage Collection question " New topic
Author

Garbage Collection question

Kamila Jolan
Greenhorn

Joined: Jul 12, 2010
Posts: 25
Hi,

The following question ask how many object are eligible for GC after line 10.



The answer is 3 but I don't know which one is the third object. To clarify it I was trying to drew the picture what is happening(but I am not sure is it correct)

line: 7. Garb gb1=new Garb(new Garb(new Garb(null))); // 3 objects created:
gb1-->|_|
gb1.g-->|_|
gb1.g.g->|_|

line: 8. gb1.g.g=new Garb(new Garb(null)); // 2 objects created, 1 eligible to GC:
gb1-->|_|
gb1.g-->|_|
gb1.g.g->|_| |_| //1st object eligible for GC
gab1.g.g.g->|_|

line: 9. Garb gb2=new Garb(new Garb(new Garb(gb1))); // Another 3 objects created:
gb1-->|_|<--gb2.g.g.g
gb1.g-->|_|
gb1.g.g->|_| |_|//1st object eligible to GC
gab1.g.g.g->|_|
gb2-->|_|
gb2.g-->|_|
gb2.g.g-->|_|

line: 10. gb2.g=gb1.g;// 2nd object eligile to GC
gb1-->|_|<--gb2.g.g.g
gb1.g-->|_|<--gb2
gb1.g.g->|_| |_|//1st object eligible to GC
gab1.g.g.g->|_|
|_|//2nd
gb2.g-->|_|
gb2.g.g-->|_|

Thus, I am not sure which is the third object available for GC.

Any help will be appreciated.

Kamila

Kacper Jonczyk
Greenhorn

Joined: Aug 25, 2010
Posts: 9
Hi Kamila,

You're almost there. At line 10 there are 2 additional objects eligible for GC instead of 1.

Gb2.g refers to the object which has a reference to another object. After changing gb2.g reference, both of them will be eligible for GC, because neither of them can be accessed any longer.

Hope this helps. Please correct me if I'm wrong. It's bit twisted example although
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

One is in the 7th line and the other is in the 10th line!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Welcome to JavaRanch

Could you please QUOTEYOURSORCES from where you get this question.


SCJP6.0,My blog Ranchers from Delhi
Kamila Jolan
Greenhorn

Joined: Jul 12, 2010
Posts: 25
Kacper, Abimaran thanks for the answer
Kacper I think you are correct.
Shanky, the question is from Deveka ExamLab simulator.

Regards,
Kamila
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Hi Guys,

This question will definitely toughen you up for the real exam, but to be clear, it's a little bit "over the top". Questions on the real exam won't be this twisty-turny.


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

Joined: Apr 15, 2010
Posts: 91

hi kamila
i am trying to draw this diagram have look


[Download gb.bmp] Download



Thanks & Regards
Sumit Kothalikar
Mehdi Ben Larbi
Ranch Hand

Joined: Aug 17, 2010
Posts: 70
Excellent diagram,it helps thanks.


SCJP 93 % ,preparing for SCWCD
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

you are welcome
Divyya Joshi
Ranch Hand

Joined: Jul 15, 2010
Posts: 102
Sumit can you please tell why g.gb1 set to null ?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

This question has been asked before like here and here. You might find the explanations there helpful as well...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection question