WHAT IS GARBAGE COLLECTION IN JAVA ?

Garbage collection is one of the most important feature of Java.Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory.
In Other words it deletes any objects that are no longer reachable by the Java program running.
The garbage collector is under the control of the JVM. The JVM decides when to
run the garbage collector.
when does an object become eligible for garbage collection?
The way to remove a reference to an object is to set the reference variable
that refers to the object to null.
Or We can also decouple a reference variable from an object by setting the reference variable to refer to another object.
e.g.
class GarbageTruck {
public static void main(String [] args) {
StringBuffer s1 = new StringBuffer("hello");
StringBuffer s2 = new StringBuffer("goodbye");
System.out.println(s1);
// At this point the StringBuffer "hello" is not eligible
s1 = s2; // Redirects s1 to refer to the "goodbye" object
// Now the StringBuffer "hello" is eligible for collection
}}
Java on calling System.gc() forces to garbage collection,
The finalize() method:
Cleaning up before garbage collection.
Java provides you a mechanism to run some code just before your object is deleted
by the garbage collector. This code is located in a method named finalize() that
all classes inherit from class Object. It is uesd when your object opened up some resources, and you'd like to close them before your object is deleted.
■ For any given object, finalize() will be called only once (at most) by the
garbage collector.
■ Calling finalize() can actually result in saving an object from deletion.

Check this program


class Address{
public void finalize () throws Throwable {
super.finalize();
System.out.println("finalize method is called");
}
}
public class demo {
public static void main(String[] args)throws Throwable{
Address a=new Address();
a.finalize();
a=null;//force garbage collection
System.gc();
System.out.println("last line");
}
}

The output is
finalize method is called
finalize method is called
last line

why finalize method is called two times ?
once when we intentionally called and
again when we force for garbage collection by System.gc()and Runtime.gc().
JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected. Garbage collection is an automatic process and can’t be forced. There is no guarantee that Garbage collection will start immediately upon request of System.gc().
Just before the object is garbage collected the finalize() of that method is called,
The finalize() method is never called more than once by the JVM for any given object, but it can be also programatically called any number of times.
  If a program uses very less memory then garbage collection is not called. All the memory will be returned to the OS once the JVM terminates.

2 comments:

  1. Good i liked the material you have given but give me some more information regarding what are the registry values which will be cleaned and what are the class loaders which will be processed first when calling system.gc() method. reply me to the mail. bye take care.

    ReplyDelete
  2. 1 correction in above article in line above "The finalize() method:"

    System.gc() only makes a "request to JVM" for garbage collection. It is upto JVM whether it will start garbage collection or not depending on the availability of HEAP memory.

    ReplyDelete