• Post Reply Bookmark Topic Watch Topic
  • New Topic

Disadvantages of Finalizers  RSS feed

 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
promptly [JLS, 12.6]. It can take arbitrarily long between the time that an object
becomes unreachable and the time that its finalizer is executed. This means that
you should never do anything time-critical in a finalizer. For example, it is a
grave error to depend on a finalizer to close files, because open file descriptors are
a limited resource. If many files are left open because the JVM is tardy in executing
finalizers, a program may fail because it can no longer open files.
The promptness with which finalizers are executed is primarily a function of
the garbage collection algorithm, which varies widely from JVM implementation
to JVM implementation. The behavior of a program that depends on the promptness
of finalizer execution may likewise vary. It is entirely possible that such a
program will run perfectly on the JVM on which you test it and then fail miserably
on the JVM favored by your most important customer.

According to the above paragrph the using finalizers are not good programming practice.Can you explain why .This pargrapgh is hard to understand due to my poor english
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To perform some postmortem cleanup on objects, one can use finalization. However, in general you should avoid using Finalizers because they are not very predictable and you don't have any control over when they will run. Therefore, you should also avoid doing any time critical thing in finalize methods.
As per doc,
You should also use finalization only when it is absolutely necessary. Finalization is a nondeterministic -- and sometimes unpredictable -- process. The less you rely on it, the smaller the impact it will have on the JVM and your application


In Effective Java, 2nd ed., Joshua Bloch says,
there is a severe performance penalty for using finalizers... So what should you do instead of writing a finalizer for a class whose objects encapsulate resources that require termination, such as files or threads? Just provide an explicit termination method, and require clients of the class to invoke this method on each instance when it is no longer needed.

In short, we should avoid finalize() as much as possible.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is online version of one of item in Effective Java which deals with Finalizer. Hope it helps.
 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thank you for your replies.So can we prevent this using Garbage collecor
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhammad Khojaye wrote:This is online version of one of item in Effective Java which deals with Finalizer. Hope it helps.
That link didn't work.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kasun Wixkramanayake wrote: . . . So can we prevent this using Garbage collecor
No. Garbage collection is one thing and finalisation something else. The finalize() method is called by the JVM when an object is removed from memory by the garbage collector. But as Bloch says, it is not deterministic and there might be a delay before execution completes.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kasun Wixkramanayake wrote:So can we prevent this using Garbage collecor

No. As Campbell mentioned, garbage collection order is not guaranteed. Even, it is not guaranteed that GC will run at all. For instance, if your program completes either by System.exit() or if all threads completes, then JVM will not called finalize() on every objects.
 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean by garbage collection order.Can you eloborate your answer
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic rule to understand regarding finalize is that you can't rely on it. You don't know when it will run or even whether it will run or not.

Let me try to explain with simple example where you open the files in the constructor and closing it in finalize method. You might think that the program works in this order and file will always be closed in the finalizer(). However, It might work in this order for one JVM but fails in another JVM where GC never finalizer.
Hope this helps.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there such a thing as garbage collection order? You cannot predict when an object is removed by garbage collection, nor whether object A is removed before object B or after, nor whether the finalisers will run in that order. Remember, if you are using a small short‑lived application, like most of the beginning forum stuff, the garbage collector may never run at all.
That is why Bloch and others tell you not to use finalisers. And you should not use this.
 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your help.So do you think finalizer always run after the garbage collection
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kasun Wixkramanayake wrote:thank you for your help.So do you think finalizer always run after the garbage collection

I suggest you to read the whole thread again. You will get your answer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!