<p>If I call <code>finalize()</code> on an object from my program code, will the <strong>JVM</strong> still run the method again when the garbage collector processes this object?</p>

<p>This would be an approximate example:</p>

<pre><code>MyObject m = new MyObject();

m.finalize();

m = null;

System.gc()
</code></pre>

<p>Would the explicit call to <code>finalize()</code> make the <strong>JVM</strong>'s garbage collector not to run the <code>finalize()</code> method on object <code>m</code>?</p>

[{'answer_id': 28856, 'author': 'John Topley', 'author_id': 1450, 'author_profile': 'https://Stackoverflow.com/users/1450', 'pm_score': 2, 'selected': False, 'text': "<p>The finalize method is never invoked more than once by a JVM for any given object. You shouldn't be relying on finalize anyway because there's no guarantee that it will be invoked. If you're calling finalize because you need to execute clean up code then better to put it into a separate method and make it explicit, e.g:</p>\n\n<pre><code>public void cleanUp() {\n  .\n  .\n  .\n}\n\nmyInstance.cleanUp();\n</code></pre>\n"}, {'answer_id': 28906, 'author': 'Tim Frey', 'author_id': 1471, 'author_profile': 'https://Stackoverflow.com/users/1471', 'pm_score': 6, 'selected': True, 'text': '<p>According to this simple test program, the JVM will still make its call to finalize() even if you explicitly called it:</p>\n\n<pre><code>private static class Blah\n{\n  public void finalize() { System.out.println("finalizing!"); }\n}\n\nprivate static void f() throws Throwable\n{\n   Blah blah = new Blah();\n   blah.finalize();\n}\n\npublic static void main(String[] args) throws Throwable\n{\n    System.out.println("start");\n    f();\n    System.gc();\n    System.out.println("done");\n}\n</code></pre>\n\n<p>The output is:</p>\n\n<blockquote>\n  <p>start<br>\n  finalizing!<br>\n  finalizing!<br>\n  done</p>\n</blockquote>\n\n<p>Every resource out there says to never call finalize() explicitly, and pretty much never even implement the method because there are no guarantees as to if and when it will be called.  You\'re better off just closing all of your resources manually.</p>\n'}, {'answer_id': 20706792, 'author': 'Ozan Aksoy', 'author_id': 1433692, 'author_profile': 'https://Stackoverflow.com/users/1433692', 'pm_score': 3, 'selected': False, 'text': '<p>One must understand the Garbage Collector(GC) Workflow to understand the function of finalize. calling .finalize() will not invoke the garbage collector, nor calling system.gc. Actually, What finalize will help the coder is to declare the reference of the object as "unreferenced".</p>\n\n<p>GC forces a suspension on the running operation of JVM, which creates a dent on the performance. During operation, GC will traverse all referenced objects, starting from the root object(your main method call). This suspension time can be decreased by declaring the objects as unreferenced manually, because it will cut down the operation costs to declare the object reference obsolete by the automated run. By declaring finalize(), coder sets the reference to the object obsolete, thus on the next run of GC operation, GC run will sweep the objects without using operation time.</p>\n\n<p>Quote: "After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded. " from Java API Doc on java.Object.finalize();</p>\n\n<p>For detailed explanation, you can also check: <a href="http://javabook.compuware.com/content/memory/how-garbage-collection-works.aspx" rel="noreferrer">javabook.computerware</a></p>\n'}]
['https://Stackoverflow.com/questions/28832', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2697/']