3

Why is finalize() not being called here. The code compiled and ran successfully but there wasn't any output.

package temp;

public class Temp {

    int i;

    Temp(int j) {
        i = j;
    }

    public void finalize() {
        if (i == 10) {
            System.out.println("Finalize called.");
        }
    }

    public static void main(String[] args) {
        Temp obj = new Temp(10);
        System.gc();
    }

}
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
daft300punk
  • 169
  • 3
  • 16

4 Answers4

6

Your call to System.gc(); makes no difference, since your Temp instance has a reference (obj) so it's not eligible for garbage collection.

Even if it was eligible for garbage collection, calling System.gc(); doesn't necessarily collect all the objects that have no reference to them immediately.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 3
    Also, Garbage collection is not guaranteed even after invoking System.gc(). Thus finalize might not be invoked. – Sid Dec 16 '14 at 06:54
  • 1
    indeed theres no guarantee. System.gc() is just **suggesting** to run the Garbage Collection . – Secondo Dec 16 '14 at 06:57
2

It so happen that im reading Effective Java

ITEM 7: AVOID FINALIZERS

Finalizers are unpredictable, often dangerous, and generally unnecessary. -Effective Java (page 50)

another one from pdf.

Don’t be seduced by the methods System.gc and System.runFinalization . They may increase the odds of finalizers ge tting executed, but they don’t guaran- tee it. The only methods that claim to guarantee finalization are System.runFi- nalizersOnExit and its evil twin, Runtime.runFinalizersOnExit . These methods are fatally flawed and have been deprecated [ThreadStop].

based on this using System.gc will only increase the odds of finalizers getting executed and importantly using it will not guarantee that it will run the Garbage Collection, it is only suggesting to the jvm.

another one.

Not only does the language specification provide no guarantee that finalizers will get executed promptly; it provides no guarantee that they’ll get executed at CHAPTER 2 CREATING AND DESTROYING OBJECTS 28 all. It is entirely possible, even likely, that a program terminates without executing finalizers on some objects that are no longer reachable

Secondo
  • 451
  • 4
  • 9
0

add obj = null; to make reference null, then your finalize method will be called. Thsi is again not a guranteed behaviour, for me 1-2 times i was able to call it out of 5 times.

 public static void main(String[] args) {
        Temp obj = new Temp(10);
        obj  = null;
        System.gc();
    }

Output

hi10
Finalize called.
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • 3
    Just to reiterate what I mentioned in another comment, System.gc() does not guarantee garbage collection. It's a matter of the runtime decision. In this case it triggered, at some other time it might not. So this is not a sure way of invoking finalize. – Sid Dec 16 '14 at 06:58
  • @reto yes correct, it was just one possibility of making reference null, but yes still not guranteed. :) – Ankur Singhal Dec 16 '14 at 07:02
  • No I tried what you said - making the reference null. But that doesn't call finalize either. I think what Sid says above is indeed the case. There just isn't a sure way to invoke finalize(). – daft300punk Dec 16 '14 at 07:04
  • @AbhilashSingh yes i agree with him as well, i ran my code , once-twice out of every 5 times i was able to call finalize method.Output did came. – Ankur Singhal Dec 16 '14 at 07:05
0

while creating an object the constructor is called but not the finalise() method so you need to refer the function from instance obj and here System.gc(); has not made any difference or called the method finalize();

Nishant Singhal
  • 21
  • 1
  • 10