2

I've the following piece of code.

class SuperClass
{
   public void finalize() throws Throwable
   {
      System.out.println("finalize() in SuperClass...");
   }
}

public class FinalizableObject extends SuperClass
{
   public static void main(String[] args) throws Exception
   {
      FinalizableObject obj = new FinalizableObject();
      Thread.sleep(1000);
      obj = null;
      System.gc();
   }

   public void finalize() throws Throwable
   {
      super.finalize();
      System.out.println("finalize() in FinalizableObject...");
   }
}

I get all 4 possible outputs

   1. finalize not called for sub/super class.
   2. finalize called for only sub class.
   3. finalize called for only super class.
   4. finalize called for both super and sub class.

... in different runs of the program. Why is it so?

My question is not that GC 'should' run on System.gc() invocation. When a sub class finalize() has been called, why doesn't the superclass finalize gets called, even though the call is explicit.

RRM
  • 2,340
  • 22
  • 36
  • 1
    Have a look at [When does System.gc() do anything](http://stackoverflow.com/questions/66540/when-does-system-gc-do-anything) – Braj Apr 29 '14 at 09:24
  • In Java Garbage collector is very lazy. If enough memory is available then it does nothing. – Braj Apr 29 '14 at 09:26
  • Call it multiple time - `for (int i = 0; i < 10; i++) { System.gc(); }` – Braj Apr 29 '14 at 09:29
  • Why do you bother too much about finalize() ? It would run only when GC runs. – Jay Apr 29 '14 at 09:34
  • If you call gc through code that doesn't mean that it will run for sure. It totally depends upon the JVM whether to run it or not and when to run it. Nothing is guaranteed when it comes to GC. JVM has very complex algorithm to decide when to run GC and for which object. Finalize method is just a chance to you to do something useful (if needed) before the object is GCed. – Ishan Rastogi Apr 29 '14 at 09:37
  • Java Language Specification does not guarantee that the JVM will start a GC when you call System.gc(). If you want an operation to peform on exit use Runtime.addShutdownHook. – Ritesh Apr 29 '14 at 09:48
  • plz check my updated question. – RRM Apr 29 '14 at 09:56

1 Answers1

0

finalize() is called before Garbage collector reclaim the Object, its last chance for any object to perform cleanup activity i.e. releasing any system resources held, closing connection if open etc.


Main issue with finalize method in java is its not guaranteed by JLS that it will be called by Garbage collector or exactly when it will be called, for example an object may wait indefinitely after becoming eligible for garbage collection and before its finalize() method gets called. similarly even after finalize gets called its not guaranteed it will be immediately collected.


Because of above reason it make no sense to use finalize method for releasing critical resources or perform any time critical activity inside finalize. It may work in development in one JVM but may not work in other JVM.

Sanjay Rabari
  • 2,024
  • 13
  • 28