• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Garbage collection..

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 952
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Punit,really good explanation.

 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JVM could have invoked the finalize() method at least once. here this states JVM could have invoked (not guarenteed).
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?


 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags and indent your code properly.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
https://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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 https://coderanch.com/t/429597/Mock-Exam-Errata/certification/Error-SJCP-Learnkey-Bonus-exam#1907357 or post #1 in this thread)

 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wolfgang Schwendt wrote:
Done.
https://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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic