Search code examples
javaarraysgarbage-collection

Is there a "finalize" method for arrays in Java?


Suppose in java, I want to do some operations when an array (e.g., int[] a = new int[3]) is garbage-collected. I know there is a finalize methods for Objects. But is there a "finalize" method for arrays?


Solution

  • finalizers in their entirety are obsolete.

    The ReferenceQueue system replaces it and can do this.

    But please be aware that garbage collection often never even happens. The VM will shut down before it ever runs out of memory. There is no guarantee that an array that is no longer reachable, is going to be removed in a few moments. It may take days. Or it really will only take a moment. You don't get guarantees.