What is the use of overriding the finalize () method of the 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 actions we perform inside our overriden finalize () method?
2.If you want to override the finalize () method if we want to manually call the JVM garbage collector?

+6
source share
8 answers

What operations do we perform inside our overriden finalize () method?

Free memory is allocated manually (through some native calls), that is, it is not controlled by the GC. This is a really rare situation. Some people there also checked that other resources related to the object have already been released - but this is for debugging purposes only, and it is not very reliable.
You must remember that:

  • finalize refers to memory and only memory.
  • You have no control when it will be called, and even whether it will be called.

So never put other resources there (for example, file descriptors, locks). All of these resources must be manually released.
And overriding termination has nothing to do with calling System.gc() .

+8
source

Others have already explained the finalize method.

Note that it is best practice to develop a close / clean / end method to close / clear an object and ask the developer to explicitly call this method. Finalizers should only be used as a safety net. (See “Item 7: Avoid Finalizers” in Joshua Bloch's “Effective Java”)

Update:

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

+6
source

Overriding the finalize method is often done if you need to manually free resources (especially those that use large amounts of memory or cause locks if they were not released). For example, if you have an object that has a lock in a file or database table, you can override your finalize method to free those objects that will be used when your object is garbage collected.

As to whether redefinition of finalization is required, this is absolutely not the case. You can call System.gc () without overriding the finalize method. One word of caution, though - calling System.gc () doesn't quite dictate that the garbage collector does anything. Consider this more as a “recommendation” for the JVM to start garbage collection rather than a “credential”.

+4
source

1) Any cleaning that you want to do when the object is destroyed (if you keep the handles open and no one caused an explicit closure, for example). Keep in mind that you do not know when this will happen, because the JVM will invoke the GC, not you.

2) No.

Please note: you should not manually call the garbage collector, see the answer here. Why don't you explicitly call finalize () or run the garbage collector?

0
source

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

  • Release system resources (such as IOStreams) as a security network, if they have not yet been closed or released.
  • Free system resources in native code that the GC does not know about.
0
source

@Andreas_D: I agree with what the riff says and that it implies that if finalize () contains code that makes the current instance unusable by GC, that is, it brings it back to life, giving the live stream a link to this instance, then to the next once this instance becomes suitable for the GC, DO NOT expect finalize () to be called by the JVM again. As for the JVM, he has done his duty by calling finalize () once and cannot be bothered by repeating this over and over again just because you are returning the instance. If that were the case, then this object would never be GC'ed. (Of course, you might want the instance to never be GC'ed. Then just use a live stream to maintain a reference to it. If no live stream has an instance reference, the instance is as good as dead.)

0
source

1. What operations do we perform inside our overriden finalize () method?

Cleaning up resources that cannot be handled by garbage collection.

2. Do we need to override the finalize () method if we want to manually call the JVM garbage collector?

No, these two things are not related.

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

-1
source

In very rare cases, it may be useful or necessary to implement finalize to free allocated resources. This method is always called when the instance is permanently destroyed.

It can be useful if you work with native code and need to free allocated resources in the area of ​​native code.

But use it with caution because the finally implementation will drastically reduce the garbage collector and kill performance.

-1
source

Source: https://habr.com/ru/post/885730/


All Articles