GARBAGE COLLECTION


I Java we can create  the object using new operator when you create object then memory will be allocated for all instance variable of class . same mechanism is available in c/c++ also.

  •   In c and c++ there are malloc() and calloc() to allocate the memory and free method to clean the memory. In c++ we have the new operator to allocate the memory and delete operator to clean the memory.
  • In java when you create object then memory will be allocated in heap because of not having any process for deallocate the memory of object. At some point heap memory will full because of this JVM is unable to create some new object in heap and JVM throws virtual error.
  •  Whenever you can create more object then JVM has to load class in stack memory when you create more object then more class will be loaded in stack memory because of this JVM is unable to load other classes in memory and then JVM throws virtual error.
                                                    Virtual machine Error
                                Stack overflow error           Out of memory error
                                 (problem with stack)            (problem with heap)
  • In java we have only new operator to allocate the memory .there is no facility for the developers to clean the memory occupied by object. memory clean will be done by JVM only with the help of Garbage Collector(GC). 
  • Garbage Collector is a service thread running behind the scence which is responsible to clean the memory as instructed by the JVM.
  • JVM identifies all unused objects in the heap time to time and prepare a list of unused object .when the size of unused object is increasing in the heap then JVM sends GC to clean all unused object.

How JVM is identifying the object is unused?

There are following three criteria to identify the object as unused.:-----
v  When you assign a null to reference variable then object referred by that reference variable  is unused and eligible for garbage collector.
                          Hello h= new Hello();
                          --------------------------
                          h= null;
v  when you assign one reference variable to another reference variable then the object which is referred by the reference variable  which is left side to assignment operator is unused and is eligible for GC.
Hello h1= new Hello();
Hello h2= new Hello();
----------------------------
h1=h2;
v  when reference variable is out of scope then object pointed by that reference variable then object pointed by that reference variable is eligible for the garbage collection(GC).out of scope means out of curly braces.{ }.

With above three criteria JVM identifying unused objects and prepares the list.

When JVM identifies memory shortage problem it invokes garbage collector with the following methods:----------

Sytem.gc();
Or
Runtime rt= Runtime.getRuntime();
Rt.gc();


As a developer you also can call gc() using system class or runtime object but there is no grantee for you call, i.e. when you call gc(),garbage collector may invoke or may not ,so we can’t force the garbage collector.

  •     Garbage collector will be invoked when JVM calls gc() , sometimes some object may refuse to clean the memory when GC is trying to clean the object. reason is , those object may be holding some resources  like network socket, IO string file, database stream, printer ,JDBC connection
  •     If you want to clean the memory of the object which are holding the resources first you have to release the resources then only GC will clean the memory of  objects without any trouble.
We need to override object class finalize method to write the resource release code or cleanup code.

For the GC management JVM will do the following things:-------------


  •     Identifying the unused object
System.RunFinalization()
Or
Runtime rt= Runtime.getRuntime();
rt.Runfinalization();


  •     RunFinalization (R.F) is responsible to invoke the finalization method on all unused object.
  •    If you want to release the resources used in your class,override the object class Finalize() in you class.
  •    When RunFinalization() called, all the resources will be released. then JVM calls GC to clean the memory.
Class Hello
{
Connection con
------------
------------
Public void Finalize()
{
          // cleanup code
Con.close();
}
}

1 comment :

  1. informative read... I'm enrolled in a JAVA Programming course @ http://www.wiziq.com/course/1617-core-java-for-beginners-icse-students and was looking online for some material that may boost my preparations.. thanks :)

    ReplyDelete