• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jtips Mock Exam #1 Question 10

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

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bill Krieger
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
??
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic