wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many object is eligible for garbage collection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many object is eligible for garbage collection?" Watch "How many object is eligible for garbage collection?" New topic
Author

How many object is eligible for garbage collection?

Morteza Manavi-Parast
Ranch Hand

Joined: Dec 25, 2008
Posts: 66
This is from Devaka's diagnostic exam. I think it should be only one , but the correct answer is two object is eligible for garbage collection. I wonder where is the second object?



SCJP 6
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3019
    
  35

Hello Morteza,

This question is asked many times in this forum. Please read this topic.

Devaka.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Yeah, this is a tricky question if you are not thinking about operator precedence, and the fact that assignment has right associativity.

A d=new A().aob=new A();
is equivalent to
A d = ((new A().aob) = new A());


All code in my posts, unless a source is explicitly mentioned, is my own.
Shin Kudo
Greenhorn

Joined: Apr 17, 2009
Posts: 25
the trick here is after the line:

the annonymous inner class object becomes eligible for GC.


SCJP 6 - SCWCD in progress...
Morteza Manavi-Parast
Ranch Hand

Joined: Dec 25, 2008
Posts: 66
Thank you!

And just to clear any doubt, there is not any "annonymous inner class" invloved. The pitfall of this question, like already is said, is the right associativity of assignment operator. That's all!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Morteza Manavi-Parast wrote:Thank you!

And just to clear any doubt, there is not any "annonymous inner class" invloved. The pitfall of this question, like already is said, is the right associativity of assignment operator. That's all!

You are correct, Morteza.

I hope your studies are going well. I (and I'm sure many other people) have missed your insightful posts (the one about inner classes was a classic.)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Shin Kudo wrote:the trick here is after the line:

the annonymous inner class object becomes eligible for GC.


Where is the anonymous inner class in this?? There is just a temporary intermediate object with no reference which becomes eligible for GC (I've marked it in bold). I hope this doesn't goes over everyone's heads

A d=new A().aob=new A();


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
yen hoang
Ranch Hand

Joined: Apr 05, 2009
Posts: 58
Ankit Garg wrote:
Shin Kudo wrote:the trick here is after the line:

the annonymous inner class object becomes eligible for GC.


Where is the anonymous inner class in this?? There is just a temporary intermediate object with no reference which becomes eligible for GC (I've marked it in bold). I hope this doesn't goes over everyone's heads

A d=new A().aob=new A();


You can say that again! There is nothing called anonymous inner class. Exactly, it's temporary object. And after all, there are two objects legal for GC. Right?


I love Mozart (^o^)
Kieren Dixon
Greenhorn

Joined: Jan 30, 2009
Posts: 27
yen hoang wrote:there are two objects legal for GC. Right?


True
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

yen hoang wrote:You can say that again! There is nothing called anonymous inner class.


Well actually I didn't see that people have already pointed it out that there is no anonymous inner class in this code, if that's what you mean. If you want to say that there is no anonymous inner class in this code, then it's fine. If you want to say that there are no anonymous inner classes in java, then I'm afraid you are wrong...
yen hoang
Ranch Hand

Joined: Apr 05, 2009
Posts: 58
Ankit Garg wrote:
yen hoang wrote:You can say that again! There is nothing called anonymous inner class.


Well actually I didn't see that people have already pointed it out that there is no anonymous inner class in this code, if that's what you mean. If you want to say that there is no anonymous inner class in this code, then it's fine. If you want to say that there are no anonymous inner classes in java, then I'm afraid you are wrong...

Yes, I made mix up. I just want to say that in that code, there is not anonymous inner class. Thanks and regards!
Morteza Manavi-Parast
Ranch Hand

Joined: Dec 25, 2008
Posts: 66
Thanks Ruben, I'm happy to hear that you found it useful!
And once again thank you for clearing my doubt on this question.
NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
Hi,
first of all thanks ruben.clearing the doubt with out any complications.
from the above discussion my understanding is, when we call the gc() it will remove the
objects which are not reffered by any reference variable and the objects
having the null value.
am i right???


I know Life is very Tough...But I AM TOUGHER
SCJP 1.5
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

NagarajGoud uppala wrote:
from the above discussion my understanding is, when we call the gc() it will remove the
objects which are not reffered by any reference variable and the objects
having the null value.
am i right???


Well it is not true. A call to gc() is just a request to the JVM to run the garbage collector. It is not sure if any objects will be garbage collected. This is why questions containing explicit call to gc() have been removed from SCJP 6.

The only thing that is sure about garbage collector is that it will run once before throwing an OutOfMemoryException. Other than that, you can't say when the garbage collector will run. In the exam, you'll only get questions asking how many objects are ELIGIBLE for garbage collection. No one will ask you how many objects are garbage collected as it cannot be determined...
NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
Hi Ankit,
thanks for your awesome explanation
you mean even gc() run explicitly or implicitly we can't make sure that all eligible objects are removed??
am i right? or correct me
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How many object is eligible for garbage collection?
 
Similar Threads
Problem in Garbage collector
Garbage Collection Question from Devaka's Exam Labs for SCJP6 - diagnostic exam question number 70.
Garbage collect question
Exam lab question on Garbage Collection
Question about Garbage collection from ExamLab diagnostic Test