0 votes
1 view
in Java by (3.4k points)
I need to know when the finalize() method is called in the JVM. I created a test class which writes into a file when the finalize() method is called by overriding it. It is not executed. Can anybody tell me the reason why it is not executing?

1 Answer

0 votes
by (27.9k points)

In usual, it's enough not to depend on finalize() to do any sweeping up, etc.

According to the Javadoc (which it would be meriting study), it is:

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

This may nevermore occur in the life of a program if the object is constantly accessible.

Additionally, the garbage collector isn't guaranteed to run at any particular time. In common, what I'm trying to say is finalize()is not the most reliable approach to use in general unless there's something definite you need it for.