my dog learned polymorphism*
The moose likes Mock Exam Errata and the fly likes Jtips Mock Exam #1 Question 10 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Jtips Mock Exam #1 Question 10" Watch "Jtips Mock Exam #1 Question 10" New topic
Author

Jtips Mock Exam #1 Question 10

Dominic Mack
Greenhorn

Joined: Sep 26, 2001
Posts: 17
Here is an exert of the question...
Which of the following are true? Select 6 answers.
�.
The finalize() method is guaranteed to be called on any object to be GC�ed
�.
The above statement is said to be true, but it is my understanding that there are �no guarantees that the objects no longer in use will be garbage collected and their finalizers executed at all.� (A Programmers Guide to Java Certification pg. 255)
Bill Tripper
Greenhorn

Joined: May 30, 2001
Posts: 24
Originally posted by Dominic Mack:

The finalize() method is guaranteed to be called on any object to be GC�ed

The key point here is "any object to be GC'ed." It's true that we cannot guarantee that any object will ever be GC'ed, but what this question is talking about is what happens once the system decides to GC something. If the JVM decides an object is to be GC'ed, the finalize method will be called, assuming it hasn't been called previously.
Bill
Dominic Mack
Greenhorn

Joined: Sep 26, 2001
Posts: 17
Thanks for the reply,
I agree the key point is "any object to be GC'ed",
but does this mean that once an object is ready to be GC'ed, it's finalize() method is guaranteed to be called?
or
is there a situation where an object that is ready to be GC'ed, but the finalize() method is never called... an Exception is thrown or System.exit, ThreadDeath, JVM crash, etc.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Dominic,
The JLS guarantees that the finalize() method will be invoked prior to an object's memory being reclaimed. It is never invoked more than once for any given object.
If an exception occurs as a result of the finalize() method, the remainder of the method is not executed and the exception is ignored.
See JLS �12.6 Finalization of Classes
Presumably if the JVM crashes object finalization will not occur.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited September 27, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Bill Krieger
Ranch Hand

Joined: Sep 27, 2001
Posts: 53
Originally posted by Dominic Mack:
does this mean that once an object is ready to be GC'ed, it's finalize() method is guaranteed to be called?

The short answer here is no. When an object is ready to be GC'd, there are still no guarantees. It's only when the system actually decides to do the garbage collection that the finalize() method will be called. An object that's ready to be GC'd might never actually be GC'd and it's finalize() method would then never be called.
Prakash Yamuna
Greenhorn

Joined: Sep 30, 2001
Posts: 5
Its not clear as to what is meant by an object is ready to be GC'ed. Are you saying it is in one of the two states:
1. Unreachable and unfinalized
2. Unreachable and finalized
An object can be GC'ed only if it is unreachable. So when you say an object is ready to be GC'ed I assume you are saying it is unreachable.
If that is the case then the finalize() method will always be called unless the JVM crashes.
From my understanding an object that is unreachable will be GC'ed eventually. While there ia no guarantee on the time period, there is a guarantee of such a service by the JVM unless it crashes.

Prakash
Bill Krieger
Ranch Hand

Joined: Sep 27, 2001
Posts: 53
Originally posted by Prakash Yamuna:

From my understanding an object that is unreachable will be GC'ed eventually. While there ia no guarantee on the time period, there is a guarantee of such a service by the JVM unless it crashes.

I disagree. See the following code:

When I run this, it creates 50,000 objects of type GcTest. By the end, 49,999 of them have become eligable for garbage collection. Far fewer than 49,999 of the finalize() methods are ever invoked. The JVM does not crash. You may need to tweek the value of 50000 depending on your available memory, in order to ensure that some objects are GC'd.
Bill
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi guys,
The guarantee is that an objects finalize() method will be called before the garbage collector releases the objects memory.
The guarantee regards the gc behaviour if and when an object is gc'd. There is no guarantee that all objects will be gc'd, even if they are eligible for gc.
The whole point of the gc is that, for the most part, you ... the programmer ... do not have to do anything special to handle memory; the gc takes care of the mundane tasks. This differs from other languages, like C, where the programmer is responsible for keeping track of, and freeing up, memory.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Dominic Mack
Greenhorn

Joined: Sep 26, 2001
Posts: 17
Thank you all for your replies.... I appreciate it very much.
If I can sum it up, the question is either poorly worded or my assumptions are wrong. Because after reviewing what everyone wrote, it seems nothing is "guaranteed" in Java because of the possiblity of a JVM crash.
So the statement:
The finalize() method is guaranteed to be called on any object to be GC�ed
is false.
Even outside of a JVM crash, it still seems like the finalize() method is still not guaranteedto be called on an object to be GC'ed, (even though the spec says it does) as demonstrated by Bill Krieger's code.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Dominic Mack:
Thank you all for your replies.... I appreciate it very much.
If I can sum it up, the question is either poorly worded or my assumptions are wrong. Because after reviewing what everyone wrote, it seems nothing is "guaranteed" in Java because of the possiblity of a JVM crash.
So the statement:
The finalize() method is guaranteed to be called on any object to be GC�ed
is false.
Even outside of a JVM crash, it still seems like the finalize() method is still not guaranteedto be called on an object to be GC'ed, (even though the spec says it does) as demonstrated by Bill Krieger's code.

Upon exiting the JVM are all objects GC'd? If so than the statement would be correct.
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
I've seen this question, and I contend that the statement is false. If an object is resurrected in the finalize() call, then when the object is about to be GC'd, the finalize() method will not be called. It is a picky distinction, but that's what these types of questions are based on.

Darryl Failla
Sun Certified Java 2 Programmer
Bill Krieger
Ranch Hand

Joined: Sep 27, 2001
Posts: 53
Originally posted by darryl failla:
If an object is resurrected in the finalize()...

If an object is resurrected in the finalize call, is subsequently made available for GC a second time, and now is ready to be GC'd...
I think we can safely guarantee that finalize() is called on any object where finalize() has been called. I don't think the wording of the question precludes the "guarantee" as referring to the invokation of finalize() that occurred before the decision to do the GC. We guarantee that finalize() gets called once when we've decided to do GC. If that happens on the first, failed attempt to GC, and not on the second successful attempt, so be it. We're only guaranteeing that finalize is called, not when it gets called.
sachin dabhade
Ranch Hand

Joined: Mar 10, 2001
Posts: 73
??
 
 
subject: Jtips Mock Exam #1 Question 10