1

So i was studying Garbage collection, how it works internally inside the java virtual machine. I came across a method called finalize(which is situated in java.lang.Object). I tried this code:

class Demo {
    int x= 10;
    int y = 20;

    public void finalize() throws Throwable {
        System.out.println("Finalize method");
    }

    static void show() {
        Demo d1= new Demo();
        display();
    }

    static void display() 
    {
        Demo d2 = new Demo();
    }
}

class Temp {
    public static void main(String args[]) throws Exception {
        Demo.show();
    }
}  

I was able to compile this code, but when I run this code there is no exception and no output.

Please assist me if you can.

I tried Google and many other reputed blogs to sort out my Problem (tutorialsPoint), (JavaRevisited).

I even searched stackoverflow, but i am unable to solve my problem.

P.S :- Output should be Finalize method which you can point out. PLEASE assist me.

To summarize, my question is: Why cant I execute the Finalize Method?

Andrew Stubbs
  • 4,322
  • 3
  • 29
  • 48
Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23
  • Your program exits before any garbage collection happens. – user253751 Jul 22 '14 at 09:56
  • `demo.show();` -- > Your program won't compile *as is*. – TheLostMind Jul 22 '14 at 09:57
  • i am Asking that is there any way to get the desired result that is Finalize method.. what if i make 1000 objects(i know this is a stupid idea who need 1000 objects) well i just want to know how exactly the garbage collection work – Deepanshu J bedi Jul 22 '14 at 09:58
  • There is no guarantee that the finalize() method is called. Besides, please format your code (and your question) properly. – flotothemoon Jul 22 '14 at 09:59
  • 1
    so here when i used System.gc 100 time(for loop ) i am able to get 9/10 outputs. – Deepanshu J bedi Jul 22 '14 at 10:14
  • @meriton sir I referred that thread before I posted this question. I never intended to open a duplicate thread.. but that thread does not contain the answer I was looking for.. sorry – Deepanshu J bedi Jul 22 '14 at 10:46
  • No problem, the duplication is not very obvious :-). But your program does exit very soon after allocating the object, and therefore before the garbage collector has run, and therefore before the finalizer is executed - just like in the linked question. Its answers should therefore be applicable. – meriton Jul 22 '14 at 11:04

3 Answers3

3

Garbage Collection in JAVA is takes care by JVM only and it is not sure when it will happen. People also uses

    System.gc();

using this also doesn't make sure that your garbage collection been happen. It just simply suggest VM that garbage collection is required.

Swaraj
  • 589
  • 4
  • 15
  • Swaraj i tried System.gc();// which is a request i believe to the jvm to collect the garbage?? i am getting 2-3 outputs per 10 runs.. – Deepanshu J bedi Jul 22 '14 at 10:09
  • yes thats correct. As I told before, garbage collector doesn't depend upon any command. Only JVM takes care of GC and it goes by algorithm written in JVM. – Swaraj Jul 22 '14 at 11:19
0

finalize method is invoked when your object is actually removed by garbage collector. Since Garbage collection is not under your control, You cannot predict when actually your method will be invoked..

Try to put some logger statement in the finalize statement, that logs out the message as soon as your object is collected by the garbage collector.

Manish Kr. Shukla
  • 4,447
  • 1
  • 20
  • 35
0

The below link would be very helpful for understanding finalize concept,

http://examples.javacodegeeks.com/core-java/java-destructor-why-is-it-missing/

sprabhakaran
  • 1,615
  • 5
  • 20
  • 36