This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection.." Watch "Garbage collection.." New topic
Author

Garbage collection..

Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi, i got it from master exam.


while the code reoresented by"// do time consuming and memory intensive stuff" is executing, which are true concerning the three Phoenix objects we know have been created?

options:

1.No Phonix object are eligible for GC.
2.Atleast one Phoenix object is eligible for GC
3.Atleast two Phoenix object are eligible for GC
4.The JVM could have invoked the finalize() method at least once.
5.The JVM could have invoked the finalize() method more than once total for these three objects
6.The JVM could have invoked the finalize() method more than twice total for these three objects
7.this code will not compile

correct Answers:
2 and 4

Explanation:

The first Phoenix object created is the only object eligible for GC. even though finalize() was invoked for that object,the JVM can invoke finalize() one more time for that object , so other options are incorrect based on the proceding.

Here is my question...

1.how come the first object is eligible,if so then the second object is also eligible.
2.how could it be sure about 4th statement(correct answer)?

it would be great if someone explain this....

Thanks in advance
Preetha
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
new Phoenix().finalize();// call it firstPhoenix

When new Phoenix() will call finalize() method, finalize() will call History.send(this); here, whatever object will call finalize(), its reference will be passed to static static void send(Phoenix p), and History class will store it as static Phoenix p2;
so static Phoenix p2=firstPhoenix

now again

new Phoenix().finalize();// call it secondPhoenix
same thing will happen that happened with firstPhoenix and after complete execution
static Phoenix p2=secondPhoenix;

Now firstPhoenix reference is not held by any reference variable, so it is elligible for garbage collection.
But secondPhoenix is not eligible for gc as its reference is held by static phoenix p2 reference.
So only one object is eligible for garbage collection. so option d.


The JVM could have invoked the finalize() method at least once.


This statement depends on the number of objects eligible while JVM is doing some time consuming task as said in question "// do time consuming and memory intensive stuff"

while the code reoresented by"// do time consuming and memory intensive stuff" is executing, which are true concerning the three Phoenix objects we know have been created?


So when JVM is executing above time consuming task at that time only one phoenix object that is firstPhoenix is eligible for garbage collection. Thats why option 4.The JVM could have invoked the finalize() method at least once.


SCJP 6
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Thanks Punit,really good explanation.

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

I read that GC for a object is done only once.
So for the first object ,finalize() done at the same point.
So how is GC done again for that object AGAIN

Or did i miss interpret the logic
:roll:


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
The JVM could have invoked the finalize() method at least once. here this states JVM could have invoked (not guarenteed).
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Garbage Collector will run finalize() method of an object at most one time.
But here 2 different objects are taking part in the program, so question is asked about how many times GC has to call finalize() if needed.

You can call finalize() manually from your program, but that is different from when GC calls finalize() method during garbage collection, so if needed GC will call finalize() method of your object but at most once. But you can call finalize() method manually 100 times.... or more.

Calling manually finalize() is like calling run() method on Thread object instead of calling start(). You can call run() on a thread object any number of time you want, but you can call start() on a thread at most one time.
Wolfgang Schwendt
Greenhorn

Joined: Jan 29, 2009
Posts: 8
Can anyone explain to me why answer 5 is false?

Initially, only the first Phoenix object is eligible for GC. But once the JVM invoked finalize() on the first Phoenix object, this object gets resurrected and becomes uneligible for GC, while now the second Phoenix object has become eligible for GC.

Therefore, it's possible that the JVM also invokes finalize() on the second Phoenix object. Thus, in _total_ it's possible that we get two finalize() calls by the JVM (of course the JVM invokes finalize() only at most once for each object).

So how come answer 5 false?


Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Hi Wolfgang,

Welcome to the forum! I actually agree with you. We might be missing something, and in that case I'm sure somebody will point it out.


All code in my posts, unless a source is explicitly mentioned, is my own.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18746
    
  40

Wolfgang Schwendt wrote:Can anyone explain to me why answer 5 is false?

Initially, only the first Phoenix object is eligible for GC. But once the JVM invoked finalize() on the first Phoenix object, this object gets resurrected and becomes uneligible for GC, while now the second Phoenix object has become eligible for GC.

Therefore, it's possible that the JVM also invokes finalize() on the second Phoenix object. Thus, in _total_ it's possible that we get two finalize() calls by the JVM (of course the JVM invokes finalize() only at most once for each object).

So how come answer 5 false?


But doesn't choice 5 say more than once for "three" objects? You only described two of the objects. How does the third object gets it's finalize method called more than once?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Henry Wong wrote:
Wolfgang Schwendt wrote:Can anyone explain to me why answer 5 is false?

Initially, only the first Phoenix object is eligible for GC. But once the JVM invoked finalize() on the first Phoenix object, this object gets resurrected and becomes uneligible for GC, while now the second Phoenix object has become eligible for GC.

Therefore, it's possible that the JVM also invokes finalize() on the second Phoenix object. Thus, in _total_ it's possible that we get two finalize() calls by the JVM (of course the JVM invokes finalize() only at most once for each object).

So how come answer 5 false?


But doesn't choice 5 say more than once for "three" objects? You only described two of the objects. How does the third object gets it's finalize method called more than once?

Henry

It actually says "more than once total" for the 3 objects. I read the "total" as in combining the number of times that finalize gets called for all three objects. In any case, if that's not what it means, the question could be worded more clearly.
Wolfgang Schwendt
Greenhorn

Joined: Jan 29, 2009
Posts: 8
Ruben Soto wrote:
It actually says "more than once total" for the 3 objects. I read the "total" as in combining the number of times that finalize gets called for all three objects. In any case, if that's not what it means, the question could be worded more clearly.


Thanks,

it could not only be worded more clearly, it should...

I for one at least, as a non-native speaker of English, had difficulties with the wording "more than once total for these three objects". I interpreted it in the way that it refers to the total number of times, finalize() might have been called for all objects (of course, at most once for a single object). And my understanding, as described above, is that -- in total -- 2 invocations of finalize() by the JVM are possible (albeit not guaranteed).
Wolfgang Schwendt
Greenhorn

Joined: Jan 29, 2009
Posts: 8
Henry Wong wrote:
But doesn't choice 5 say more than once for "three" objects? You only described two of the objects. How does the third object gets it's finalize method called more than once?


I interpreted it in the following way:

We have 3 objects, of which the third never becomes eligible for GC, hence also never for a call of finalize() by the JVM.

But... For 2 of those 3 objects, respectively, a call of finanlize() is possible. So if we count the total number of possible finalize() invocations by the JVM (at most once for a single object), the answer is 2. Thus, I thought that answer 5 (more than once) is correct.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Wolfgang Schwendt wrote:
Thanks,

it could not only be worded more clearly, it should...

I for one at least, as a non-native speaker of English, had difficulties with the wording "more than once total for these three objects". I interpreted it in the way that it refers to the number of times, finalize() might have been called for all objects (of course, at most once for a single object). And my understanding, as described above, is that 2 invocations of finalize() by the JVM are possible (albeit not guaranteed).

I honestly think it's a mistake, and Answer 5 should be correct as well.
Wolfgang Schwendt
Greenhorn

Joined: Jan 29, 2009
Posts: 8
Ruben Soto wrote:
I honestly think it's a mistake, and Answer 5 should be correct as well.


I think it's a mistake, too, and that's why I posted.

Anyway, nice that I'm not the only one who views it as a mistake...
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Option 5 is wrong, it is not a mistake, as question is explicitly asking this:
while the code reoresented by"// do time consuming and memory intensive stuff" is executing, which are true concerning the three Phoenix objects we know have been created?

Means the code is running just below the

It has not completely executed.
So first phoenix object is eligible for garbage collection.
Second phoenix object will be referenced by static Phoenix p2; of class History.
Third phoenix object will be reference by p3.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Hi Punit,

So the first object is eligible for garbage collection. This means that while the time-consuming code is executing, the JVM might call the finalize() method on that first object. This will cause the first object to be referred to from History, and the second object in turn to be eligible for GC. Then, the JVM might call finalize on the second object, which will become referenced by History, and the first object will again become available for GC. Am I missing something?

Thanks.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Please use code tags and indent your code properly.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ruben Soto wrote:Hi Punit,

So the first object is eligible for garbage collection. This means that while the time-consuming code is executing, the JVM might call the finalize() method on that first object. This will cause the first object to be referred to from History, and the second object in turn to be eligible for GC. Then, the JVM might call finalize on the second object, which will become referenced by History, and the first object will again become available for GC. Am I missing something?

Thanks.


Yes you are right Ruben, GC may be called 2 times for both the phoenix object.


Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Punit,

Thanks for putting the time to confirm this, great troubleshooting code.

Good for Wolfgang also for noticing this error. Wolfgang, feel free to file an errata report here: Mock Exam Errata

Aren't the Master Exam questions from the same pool of questions as the SCJP exam? If so, this might be a high priority fix.
Bert Bates
author
Sheriff

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

It might be that there is an error here but I'd like to ask you to summarize your concern - please post the entire question with the summary of your argument - thanks! When you post the question please don't change ANY of the code or the wording - remember, not everyone has a copy of the book, and if you change even a single word of the original question it might make a change that you're not expecting - just like real code

Also, don't worry, the questions in the book or on the book's master exam CD or on the McGraw Hill website are TOTALLY DIFFERENT than the questions on the real Sun exam. There is no overlap, I promise

hth,

Bert


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

Joined: Dec 16, 2008
Posts: 1032
Bert Bates wrote:Hi Guys,

It might be that there is an error here but I'd like to ask you to summarize your concern - please post the entire question with the summary of your argument - thanks! When you post the question please don't change ANY of the code or the wording - remember, not everyone has a copy of the book, and if you change even a single word of the original question it might make a change that you're not expecting - just like real code

Also, don't worry, the questions in the book or on the book's master exam CD or on the McGraw Hill website are TOTALLY DIFFERENT than the questions on the real Sun exam. There is no overlap, I promise

hth,

Bert

Thanks very much for clarifying that, Bert.

I don't have MasterExam installed because I'm using Linux at the moment (will install MasterExam in a few days in a different computer.)

Wolfgang, can you take care of gathering the information? Let's make sure the actual question wasn't changed in any way, because as Bert says a simple word change can make a big difference.
Wolfgang Schwendt
Greenhorn

Joined: Jan 29, 2009
Posts: 8
Ruben Soto wrote:
Wolfgang, can you take care of gathering the information? Let's make sure the actual question wasn't changed in any way, because as Bert says a simple word change can make a big difference.


Done.
http://www.coderanch.com/t/429597/Mock-Exam-Errata/certification/Error-SJCP-Learnkey-Bonus-exam#1907357

The filed error report includes the original wording from the Learnkey exam.

Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Okay, I'll probably regret being dense here, but just to confirm:

Is it clear to everyone the distinction between when the programmer explicitly invokes finalize() vs. when the JVM chooses to call finalize()?
Wolfgang Schwendt
Greenhorn

Joined: Jan 29, 2009
Posts: 8
Bert Bates wrote:Okay, I'll probably regret being dense here, but just to confirm:

Is it clear to everyone the distinction between when the programmer explicitly invokes finalize() vs. when the JVM chooses to call finalize()?


yes, perhaps not to everyone, but at least to the users who discussed the mentioned error in this thread, the distinction between an explicit invocation of finalize() and an invocation by the JVM is absolutely clear.

However, some confusion arose regarding what is exactly meant with the wording of the option"The JVM could have invoked the finalize() method more than once total for these three objects ", in particular the word "total". Although my interpretation was correct, as it turned out now, I was confused against the backdrop that the Learnkey solution erroneously claimed that this option is false.
If total refers to the total number of times the JVM (not the programmer) might invoke finalize() on (different) Phoenix objects, the result is that "2 invocations are possible (albeit not guaranteed)" which is also proven by Punit Singh's example program.

(For the original question with all answer options see http://www.coderanch.com/t/429597/Mock-Exam-Errata/certification/Error-SJCP-Learnkey-Bonus-exam#1907357 or post #1 in this thread)

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18746
    
  40

Wolfgang Schwendt wrote:
Done.
http://www.coderanch.com/t/429597/Mock-Exam-Errata/certification/Error-SJCP-Learnkey-Bonus-exam#1907357

I also added the original question from the Learnkey exam.




Sorry, but I don't agree with this. Maybe it is because I *am* a native english speaker, and implicitedly added an "each" in front of the "three objects".... but it is more likely because, the question is discussing how the JVM may call the finalize() method for any given object, so to imply "total" to mean all objects as a sum, when the premise is to test the amount of calls per object, just doesn't seem correct to me.

IMHO, at best, the wording could be a bit more percise... maybe adding the word "each" would help here.

Henry
denis sorn
Ranch Hand

Joined: Apr 30, 2008
Posts: 33
Punit Singh wrote:Garbage Collector will run finalize() method of an object at most one time.
But here 2 different objects are taking part in the program, so question is asked about how many times GC has to call finalize() if needed.

You can call finalize() manually from your program, but that is different from when GC calls finalize() method during garbage collection, so if needed GC will call finalize() method of your object but at most once. But you can call finalize() method manually 100 times.... or more.

Calling manually finalize() is like calling run() method on Thread object instead of calling start(). You can call run() on a thread object any number of time you want, but you can call start() on a thread at most one time.


Hi Punit,

could you please explain why GC runs finalize() method?
Wolfgang Schwendt
Greenhorn

Joined: Jan 29, 2009
Posts: 8
denis sorn wrote:
could you please explain why GC runs finalize() method?


read and try to understand the contract for finalize()
http://java.sun.com/javase/6/docs/api/java/lang/Object.html#finalize()

The Javadocs contain a very good description.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Dang! You guys got me!

This is the first time in a while I've had to break out the compiler to test a possible (now verified), errata!

So, now, who can explain why the 2nd Phoenix object can become GC-able?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Bert Bates wrote:Dang! You guys got me!

This is the first time in a while I've had to break out the compiler to test a possible (now verified), errata!

So, now, who can explain why the 2nd Phoenix object can become GC-able?

Hi Bert,

Originally, the second object (the one on which finalize() is invoked in the code last) is not elligible for GC, since History.p2 refers to it. The first object is eligible for GC however. When the JVM calls finalize on the first object, it makes History.p2 refer to it. At this point the first object is not eligible for GC, but the second object is, since History.p2 doesn't refer to it anymore. Finally, the JVM will call finalize() on the second object, leaving the first object once again eligible for GC. The second object will not get garbage collected based only on the code we are offered, but the first object should be (although the JVM won't call finalize() on it again.)

This is a little tricky, and you need to keep count of which object is referred to by p2, and how many times finalize() has gotten called by the JVM on each object.
denis sorn
Ranch Hand

Joined: Apr 30, 2008
Posts: 33
Wolfgang Schwendt wrote:
denis sorn wrote:
could you please explain why GC runs finalize() method?


read and try to understand the contract for finalize()
http://java.sun.com/javase/6/docs/api/java/lang/Object.html#finalize()

The Javadocs contain a very good description.


Thanks Wolfgang, up to now I managed to avoid fiddling with GC.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection..