user121196
user121196

Reputation: 30990

Java: ill defined finalize method to create memory leak

In java the garbage collect will invoke finalize method on an object x if there's no strong reference pointing to x and x is eligible for garbage collection. What if the finalize method never terminates, would this cause a memory leak?

public class X{
  protected void finalize(){
     while(true){}
  }
}

Upvotes: 8

Views: 661

Answers (4)

leef
leef

Reputation: 593

It would block the java.lang.ref.Finalizer$FinalizerThread (The Secret Life Of The Finalizer), the instance of all classes which implement finalize method would be blocked in java.lang.ref.Finalizer.ReferenceQueue. You continue creating new object with 'finalize', memory would be exhausted by those objects which are waiting to execute finalize . If take a heap dump, you would see objects are retained by java.lang.ref.Finalizer, see below example (it's a real case). enter image description here

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

Yes it will, easy to test

public class X {

    protected void finalize() {
        while (true) {
        }
    }

    public static void main(String[] args) throws Exception {
        while (true) {
            new X();
        }
    }
}

after some time I got

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"

when I removed finalize() the test never stopped. Note that it takes a while before JVM goes OOM

BTW it's enough to run this test

public class X {
    byte[] a = new byte[100 * 1000 * 1000];

    protected void finalize() {
        System.out.println();
    }

    public static void main(String[] args) throws Exception {
        while (true) {
            new X();
        }
    }
}

to break GC

Exception in thread "main" 
java.lang.OutOfMemoryError: Java heap space
    at test.X.<init>(X.java:5)
    at test.X.main(X.java:13)

comment out //System.out.println(); and it works non-stop

Upvotes: 6

Ajinkya
Ajinkya

Reputation: 22710

Yes.
Also inside finalize method if you give valid reference to object on which finalize method is invoked, Java will not garbage collect the object also will not invoke the finalize method again as it is invoked only once.

Upvotes: 2

Renjith
Renjith

Reputation: 3274

Definitely.The memory will be deallocated after finalise method returns.And if your finalise never returns, momory will not get deallocated.

Google about resurrection in garbage collection and you will get various instances where finalise method does not guarantee the gc

Upvotes: 2

Related Questions