9

Possible Duplicate:
method finalize and exceptions

The garbage collector invokes finalize() method when an object is about to released from memory. What happens when an exception is raised in the finalize() method?

Will GC proceed further and release the memory, or GC will halt the process for that object?

2
  • 6
    Why are you writing finalize() methods in your Java code? It is bad practice in most every case.
    – user177800
    Jun 27, 2012 at 19:32
  • 2
    No I haven't written such method , just for my knowledge
    – Kumar
    Jun 27, 2012 at 19:40

2 Answers 2

11

If the exception is raised in a try-catch statement and is correctly caught then yes, "the GC will proceed further", as it does in regular programs.

But if the thrown exception is not handled then, according to the JSL,

If an uncaught exception is thrown during the finalization, the exception is ignored and finalization of that object terminates.

So, in this case the "GC will halt the process for that object" and in which case it may be that some its resources are not have been correctly released.

By the way, the finalizer has 2 drawbacks :

  • You cannot be sure that the finalize method will be ever be executed (from this blog)

    Why do the above if the garbage collector will always call finalize before freeing up memory associated with an object? The reason is because the garbage collector doesn't always get a chance to clean up objects before the JVM terminates.

  • Finalizers force objects to be promoted to old space (that takes longer to collect), and postpone the GC until the finalizer is run, adding more overhead to the GC algotithm. (cf Java performance Tuning 2nd,Edition by Jack Shirazi).

5
  • Yes, I feel you are right. Isn't that cheating the GC. What I mean is GC is in process means if and only memory has no more reference. If the memory of the Object holds the reference of another Object is that eligible for GC. If not what is the exact purpose of the finalize method?
    – Kumar
    Jun 27, 2012 at 21:35
  • 1
    @Kumar Finalize method is not used to "force" an object to be collected, it's used to free up resources that cannot be freed automatically by an automatic storage manager. I never use this I guess it's usefull in some rare case. As you can see in the blog link, you can often just use a finally statement and release ressource here (for exemple close InputStream, Reader etc...). Jun 27, 2012 at 22:11
  • I think I will have to re frame my comment, it is like this, GC is in process and an Object memory is about reclaimed. It has got the finilaze method. If it throws an exception then GC would not clear the memory, is this not loophole in java. And next question is , generally people are writing that "finalize" method is used clean up the resource, So my question is if One object is eligible for GC will that have a reference to any resource, if so how will that be treated as no reference by GC.
    – Kumar
    Jun 28, 2012 at 6:33
  • 1
    @Kumar I think you're wrong, if an exception occurs, finalization don't end properly, but I guess the object can be collected anyway if there is no more strong reference handled. By the way it should not happend, if you really want to use finalize be sure that you're code is clean... For your second question, the aim is to clean reference so when you close all your ressources there is no more reference and the object can be GC. It also make me think of "object island", take a look : stackoverflow.com/q/10861231/1140748 Jun 28, 2012 at 8:09
  • 1
    The blog link is broken
    – Yang Bo
    Feb 9, 2017 at 3:33
0

"Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored." http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#finalize()

4
  • the To be clear summary is wildly incomplete, inaccurate and misrepresents what actually happens.
    – user177800
    Jun 27, 2012 at 19:53
  • Please join the discussion below to explore more
    – Kumar
    Jun 27, 2012 at 21:38
  • @Jarrod: you're right, sorry - my answer was rushed. I was trying to get across my understanding that the object will still be collected if an exception is thrown. I have removed the unhelpful summary. Jun 28, 2012 at 8:42
  • @Kumar just for the record this site isn't for discussion, it is for questions and answers. discussion is strictly off topic
    – user177800
    Jun 28, 2012 at 12:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.