Garbage collection and finalize method


Garbage Collection is the process of JVM in which, Objects those are not referenced anymore, are destroyed automatically. The garbage collection is the automatic memory management process in JVM.

It can not be forced to reclaim memory by program using method. It runs automatically when Runtime require memory.

When any object is destroyed by garbage collector, the finalize method of object is invoked before destroy of object.

Programmer can override this method to do some useful task like following program.

//How to use finalize method

class A{
    byte b1[][]=new byte[1024][1024];//1MB
    byte b2[][]=new byte[1024][1024];//1MB
    
    protected void finalize(){
        System.out.println("good Bye");
    }
    
}
class JavaDemo63{
    
    public static void main(String ar[]){
        A a=new A();
        a=new A();
        a=new A();
        a=new A();
        a=new A();
        a=new A();
        a=new A();
        a=new A();
    }
}


No comments:

Popular Posts