简体   繁体   中英

What is the use of overriding the finalize() method of Object class?

As far as I know , in java if we want to manually call the garbage collector we can do System.gc().
1.What are the operations that we do inside our overriden finalize() method?
2.Is it required to override the finalize() method if we want to manually call the JVM garbage collector?

What are the operations that we do inside our overriden finalize() method?

Free memory alocated manually (through some native calls) ie not managed by GC. It is a really rare situation. Some people put there also a check, that other resources connected with the object have already been released - but it's only for debugging purposes and it's not very reliable.
You must remember that:

  1. finalize is about memory and only memory.
  2. You have no control when it will be invoked and even whether it be invoked.

So never ever put there releasing any other resources (like file handles, locks). All those resources must be released manually.
And overriding finalize has nothing to do with calling System.gc() .

Others already explained the finalize method.

Note however, that best practices is to design a "close"/ "clean-up"/ "termination" method to close/ clean-up the object and ask the developer to call this method explicitly. Finalizers should only be used as a safety net. (See "Item 7: Avoid finalizers" of "Effective Java" by Joshua Bloch)

Update:

In this case also consider to implement AutoCloseable to support try-with-resources blocks.

Overriding the finalize method is often done if you need to manually release resources (particularly those that would use large amounts of memory or cause locks, if they weren't released). For example, if you have an object that has a lock on a file or database table, you may want to override your finalize method in order to release those objects for use when your object is garbage collected.

As for whether or not overriding finalize is required, it absolutely is not. You can call System.gc() without ever overriding a finalize method. One word of caution, though - calling System.gc() doesn't absolutely dictate that the garbage collector will do anything. Consider it more of a "recommendation" to the JVM to run garbage collection, as opposed to a "mandate".

For very rare cases it may be useful or necessary to implement finalize for freeing allocated resources. This method is always called when an instance is destroyed finally.

May be helpful if you work with native code and need to free allocated resources in the native code area.

But use it with care, because implementing finally will slow down the garbage collector dramatically and kill performance.

1) Any clean up that you want to do on object destruction (if you keep some handles open and nobody has called an explicit close for example). Keep in mind that you don't know when this will happen because the JVM will call the GC and not you.

2) Nope.

Side note, you should not manually call the Garbage Collector, see the answer here, Why do you not explicitly call finalize() or start the garbage collector?

There is no guarantee that the finalizer will be called promptly so you should not do anything critical in your finalize method.

  1. Release system resources (Such as IOStreams) as a safety net if they weren't already closed or released.
  2. Release system resources iin native code that the GC doesn't know about.

@Andreas_D: I agree with what reef says and what he implies is that, if finalize() contains code that makes the current instance ineligible for GC, ie brings it back to life by giving a live thread a reference to this instance, then the next time that that instance becomes eligible for GC, DO NOT expect finalize() to be called by the JVM again. As far as the JVM is concerned, it has done its duty by calling finalize() once and cannot be bothered to do it again and again just because you keep bringing the instance back to life again. If it did, then that object would never be GC'ed. (Of course, you might want the instance never to be GC'ed. Then just have a live thread maintain a reference to it. If no live thread has a reference to the instance, the instance is as good as dead, anyway.)

1.What are the operations that we do inside our overriden finalize() method?

Cleaning up resources that cannot be handled by garbage collection.

2.Is it required to override the finalize() method if we want to manually call the JVM garbage collector?

No, these two things are unrelated.

finalize should certainly not call the garbage collector itself. In fact, finalize is called by the garbage collector (but is not guaranteed to be called at all).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM