Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The spec writes:</p> <blockquote> <p>Before the storage for an object is reclaimed by the garbage collector, the Java Virtual Machine will invoke the finalizer of that object.</p> <p>The Java programming language does not specify how soon a finalizer will be invoked, except to say that it will happen before the storage for the object is reused. </p> </blockquote> <p>I read this to mean that the finalizer must have completed before the storage may be reused.</p> <blockquote> <p>The Java programming language does not specify which thread will invoke the finalizer for any given object. </p> <p><em>It is important to note that many finalizer threads may be active (this is sometimes needed on large shared memory multiprocessors), and that if a large connected data structure becomes garbage, all of the finalize methods for every object in that data structure could be invoked at the same time, each finalizer invocation running in a different thread.</em> </p> </blockquote> <p>That is, finalization may occur in the garbage collector thread, in a separate thead, or even a separate thread pool.</p> <p>A JVM is not permitted to simply abort executing a finalizer, and can only use a finite number of threads (threads are operating system resources, and operating systems don't support arbitrarily many threads). Non-terminating finalizers will therefore of necessity starve that thread pool, thereby inhibit collection of any finalizable objects, and cause a memory leak.</p> <p>The following test program confirms this behavior:</p> <pre><code>public class Test { byte[] memoryHog = new byte[1024 * 1024]; @Override protected void finalize() throws Throwable { System.out.println("Finalizing " + this + " in thread " + Thread.currentThread()); for (;;); } public static void main(String[] args) { for (int i = 0; i &lt; 1000; i++) { new Test(); } } } </code></pre> <p>On Oracle JDK 7, this prints:</p> <blockquote> <pre><code>Finalizing tools.Test@1f1fba0 in thread Thread[Finalizer,8,system] Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at tools.Test.&lt;init&gt;(Test.java:5) at tools.Test.main(Test.java:15) </code></pre> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload