aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how many objects are eligible for GC 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 "how many objects are eligible for GC" Watch "how many objects are eligible for GC" New topic
Author

how many objects are eligible for GC

Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

hello everone,

i am having some doubt regarding this question in k&b book page 269 first question .


so, my question is, as cb reference in the go() method is assigned null, it returns null and this makes c3 reference null. is c3 eligible for GC as go() returned null ?
thanks in advance...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

c3 is a reference, and it's objects that are garbage collected, not references. So c3 can't be eligible.

What you need to ask is:
- how many objects were created?
- how many of those do we still hold a reference to?
Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

oh crap my mistake mate.... i should read out the basics again.
and total 3 objects were created and only one object has a live reference in this case right ?
any ways thanks for the reply.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

3?

You're creating two CardBoard objects (two new statements). Each of those also contains a Short (200 is out of the range where Shorts are cached, I think). That makes four objects created.

You end up with one live reference to a CardBoard object (c2), but that's still got a reference its Short.


[I'm assuming that ClassBoard = CardBoard - otherwise the answer is "does not compile" ]
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Matthew Brown wrote:3?

You're creating two CardBoard objects (two new statements). Each of those also contains a Short (200 is out of the range where Shorts are cached, I think). That makes four objects created.

You end up with one live reference to a CardBoard object (c2), but that's still got a reference its Short.


[I'm assuming that ClassBoard = CardBoard - otherwise the answer is "does not compile" ]


Ikpefua wrote:

Hello Chaitanya, in addition to Matthews fantastic explanation, I want to mention that garbage collection gave me SERIOUS headache till the day I discovered that seeing events as they happen in a diagram is about the best way to turn Garbage collection into a piece-of-cake (I repeat piece-of-cake). Study CAREFULLY the attached diagram and tell me what you think.

Hint: REMEMBER that objects are ELIGIBLE for garbage collection when there is NO live thread (reference variable) that can reach it (FROM OUTSIDE THE HEAP).

Be very free to reply me with any doubts regarding this diagram I will be pleased to assist.

One more thing drawing this diagram with 'paint' took me only 5mins, and with pencil and paper, it takes 1min.
If you are NOT good at drawing, simply practice everyday, trust me it will save you the headache of trying to
resolve garbage collection by simply studying the lines of codes.

I hope this helps.



[Thumbnail for CardBoard_Heap.png]



OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Jean-Francois Alban
Greenhorn

Joined: Apr 13, 2011
Posts: 5
Matthew Brown wrote:3?

You're creating two CardBoard objects (two new statements). Each of those also contains a Short (200 is out of the range where Shorts are cached, I think). That makes four objects created.

You end up with one live reference to a CardBoard object (c2), but that's still got a reference its Short.


[I'm assuming that ClassBoard = CardBoard - otherwise the answer is "does not compile" ]


ClassBoard... good catch man!!
Suhas Mandrawadkar
Ranch Hand

Joined: Jul 21, 2007
Posts: 72

Matthew Brown wrote:3?

You're creating two CardBoard objects (two new statements). Each of those also contains a Short (200 is out of the range where Shorts are cached, I think). That makes four objects created.

You end up with one live reference to a CardBoard object (c2), but that's still got a reference its Short.


[I'm assuming that ClassBoard = CardBoard - otherwise the answer is "does not compile" ]


Can you ellaborate that concept please


Regards, Suhas S. Mandrawadkar.
Certifications: SCJP 6, SCWCD 5, Oracle WebLogic Server Administrator, OCE Java EE 6 EJB Developer
Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

Matthew Brown wrote:3?

[I'm assuming that ClassBoard = CardBoard - otherwise the answer is "does not compile" ]


oops...!!! hahaha, my mistake lads... lol
Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

Ikpefua Jacob-Obinyan wrote:
Matthew Brown wrote:3?

You're creating two CardBoard objects (two new statements). Each of those also contains a Short (200 is out of the range where Shorts are cached, I think). That makes four objects created.

You end up with one live reference to a CardBoard object (c2), but that's still got a reference its Short.


[I'm assuming that ClassBoard = CardBoard - otherwise the answer is "does not compile" ]


Ikpefua wrote:

Hello Chaitanya, in addition to Matthews fantastic explanation, I want to mention that garbage collection gave me SERIOUS headache till the day I discovered that seeing events as they happen in a diagram is about the best way to turn Garbage collection into a piece-of-cake (I repeat piece-of-cake). Study CAREFULLY the attached diagram and tell me what you think.

Hint: REMEMBER that objects are ELIGIBLE for garbage collection when there is NO live thread (reference variable) that can reach it (FROM OUTSIDE THE HEAP).

Be very free to reply me with any doubts regarding this diagram I will be pleased to assist.

One more thing drawing this diagram with 'paint' took me only 5mins, and with pencil and paper, it takes 1min.
If you are NOT good at drawing, simply practice everyday, trust me it will save you the headache of trying to
resolve garbage collection by simply studying the lines of codes.

I hope this helps.


hello Ikpefua Jacob-Obinyan ,

That was fantastic explanation mate. thanks a lot. so, two Short objects are created for each instance of the class, so, total 4 objects are created and two objects are eligible for GC. (C1 and its associated Short object). please let me know if i am wrong (again)..
and i really thank you for depiction. it really helped me ..
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Chaitanya Kida wrote:so, two Short objects are created for each instance of the class.


Hello Chaitanya, I guess the above analysis is a mistake or simply a typo-graphical error, just in case let me correct you by saying that 'one Short object' (NOT TWO) is ASSOCIATED to every instance of the CardBoard class.

(Oops Or Was It ClassBoard class??. .... ).......
Chaitanya Kidambi
Ranch Hand

Joined: Mar 05, 2011
Posts: 47

Ikpefua Jacob-Obinyan wrote:
Chaitanya Kida wrote:


so, two Short objects are created for each instance of the class.

Ikpefua wrote:


Hello Chaitanya, I guess the above analysis is a mistake or simply a typo-graphical error, just in case let me correct you by saying that 'one Short object' (NOT TWO) is ASSOCIATED to every instance of the CardBoard class.

(Oops Or Was It ClassBoard class??. .... ).......


hello Ikpefua Jacob-Obinyan,
sorry, my mistake(again) . one Short object for each instance.
thank you
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
Right, I've got a question. In this exercise (and others of this type), how do I know that when //doStuff is reached the two objects haven't already been garbage-collected.

Since we can't predict when GC will run, can I assume it hasn't run and the objects are still eligible?


OCA, OCP Java 6
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Chandella Montero wrote:Right, I've got a question. In this exercise (and others of this type), how do I know that when //doStuff is reached the two objects haven't already been garbage-collected.

Since we can't predict when GC will run, can I assume it hasn't run and the objects are still eligible?


Hello Chandella, in java the moment of an objects "garbage-collection" has NO guarantee, the ONLY guarantee is the moment of "eligibility"...Read CAREFULLY everything posted here you will notice that the word "ELIGIBLE" is what is used here and not "GARBAGE-COLLECTED"...And thats all you need to know for the purpose of the exams.

I hope this helps

Regards

Ikpefua
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
Hi Ikpefua! Exactly, so... if an object has become eligible and has been garbage collected almost immediately, in theory, it isn't eligible anymore when the code reaches that point, right? I know it's being incredibly fussy, but I remember this being one of the catches in Mughal's book (can't remember which exercise it was now).
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
For the purposes of the exam, can we safely disregard this possibility?
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Chandella Montero wrote:if an object has become eligible and has been garbage collected almost immediately


If there is NO guarantee that when an object is eligible it is garbage collected...answering your question has no guarantee neither ...I repeat the ONLY guarantee is that when an object has no live reference variable(live thread)access it becomes 'ELIGIBLE' for garbage collection, when will it be collected? the moment is NOT guaranteed AND that is NOT on the exams, what is on the exams is when the object will be 'ELIGIBLE'...When will it be 'ELIGIBLE'?...Like you already know as mentioned above.

Regards

Ikpefua.
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
Hmmm... I think I get your point. Even if the object has been garbaged collected straight away, it has at some point become eligible. So the question should really ask "how many objects will have become eligible for GC?". My God, you've got to be so careful with the wording of everything! Thanks for your clarification Ikpefua
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
It's a pretty good question for sure. In the past, the exams were intentionally "trickier" than they are now. So it's possible that in the past Sun might have tried to catch you on that tiny little nit. In general, that's not the case any more. For GC questions, Oracle / Sun wants you to understand when object become eligible - it's all about objects and reference variables. So, for the exam, assume that the GC doesn't run while the listed code is executing. In other words, take the question at face value - no tricks.

hth,

Bert


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

Joined: Feb 18, 2011
Posts: 89
Cheers, Bert. I will keep that in mind.

This exam is making me almost paranoid with the wording of everything question I read: "right, now where is the catch?! I know you're trying to trick me... I know..."
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Chandella Montero wrote:
"right, now where is the catch?! I know you're trying to trick me... I know..."


Chandella...I "maybe" misuderstanding you here...but there are NO tricks with garbage collection...SIMPLY burn-it-in that objects without reference variable access are eligible for garbage collection, and thats what you will be asked in the exams... For Example;

How many objects are eligible for garbage collection when execution gets to line 10?


Did you get 'the-catch' ?...Yes you did ...Take a second look at the question and there's the word again 'ELIGIBLE'!.

And one more thing VERY important...If you really want to smile with garbage collection questions in the exams (dont be paranoid NORMALLY there will be one or two questions regarding this topic)...Go and practice diagram drawing...look up this thread, download the diagram I placed there.
Any garbage collection question you find, use diagrams to find out how many objects are 'ELIGIBLE'... for garbage collection. If you get stuck come here and lets resolve it together...I have posted SEVERAL diagrams in this forum on G.C. topics search for them.

Regards

Ikpefua.

Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
But Ikpefua, the garbage collector does eventually run! After it runs, the objects are no longer eligible, they are gone
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Chandella Montero wrote:But Ikpefua, the garbage collector does eventually run! After it runs, the objects are no longer eligible, they are gone

Chandella effectively! ...There is NO doubt about that...But for the purpose of the exams here is a caption of what Author/Sherrif Bert Bates mentioned in this thread says quote
So, for the exam, assume that the GC doesn't run while the listed code is executing.


If you still aren't convinced, well thats NO good news for the garbage collection questions you may encounter on the exams.

Regards

Ikpefua
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89


I'm more than happy to go by that! Just wanted to make sure
Isuru Rangana
Greenhorn

Joined: Jun 20, 2011
Posts: 5

2
One of CardBoard object refer from c1;
Another is Short wrapper Object in CardBoard object which is hang on by c1;


class CardBoard {
Short story =200;
CardBoard go(CardBoard cb) {
cb=null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2= new CardBoard();
CardBoard c3 = c1.go(c2);
c1=null;
//doStuff
System.gc();
}
public void finalize(){
System.out.println("CardBoard");
}
}

am i correct guys???
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how many objects are eligible for GC