Tuesday, November 17, 2015

Garbage Collection in Java

Introduction:

  In C++ the programmer is responsible for both creation and destruction of objects.
  But usually the programmer is giving very much importance for creation of objects
  and he is ignoring the destruction of objects.
    
 But in java programmer is responsible only for the creation of objects but sun people  introduced one assistance which is running continuously in the background for destruction of  objects.

 Due to this assistance, there is no chance of failing the java program due to memory problem.This assistance is nothing but"Garbage Collector". 

Advantage of Garbage Collection:

  1. It make java memory efficient because garbage collector removes the unreferenced objects from heap memory.
  2. It is automatically done by the garbage collector so we do not need any extra effect

The ways to make an object eligible for garbage collector:

Even though the programmer is not responsible for destruction of object it is good programming practice to make an object eligible for garbage collector if it is not no longer required.

Nullifying The reference Variable:

If an object is no longer assign null to all its reference variables
                    
Ex:  Student s1=new Student();
        Student s2=new Student();

  No object is eligible for garbage collector

 s1=null; // s1 is eligible for garbage collector
 s2=null;// s2 is eligible for garbage collector

 Note: 
Now, Both objects eligible for garbage collector 

Reassigning The reference variable: 
               
Ex:  Student s1=new Student();
        Student s2=new Student();
              
 No object is eligible for garbage collector

 s1=s2;// one object is eligible for garbage collector

                                           
Note:  Now First object refereed by s1 is eligible for garbage collector

The objects are created inside a method:

The objects are which are created inside a method are by default eligible for garbage collector once method completes.

Ex:
class Test{
public static void main(String args[]){
m1();
//both the objects are eligible for garbage collector 
}
public static void m1(){
Student s1=new Student();
Student s2=new Student();
}
}

Finalize() method:

This method is invoked each time before the object garbage collected. This method 
can be used  to perform cleanup processing.

This method is defined in Object class as: 
 Protected void finalize(){
 }

Case 1:

Garbage Collector always calls finalize() on the Object which is eligible for Garbage Collector and the corresponding class finalize method will be executed.

Ex:
class Test{
public static void main(String arg[]){
String s = new String("raju");
//Test s = new Test();
s = null;
System.gc();
System.out.println("end of main method");
}
public void finalize(){
System.out.println("finalize method called");
}
}
gc() method: 

The gc() method is used to invoke the garbage collector to perform cleanup processing.
The gc() is found in System and Runtime classes.

Simple Example for garbage collection in java:

Ex:
public class GarbageDemo{
public void finalize() {
System.out.println("object is garbage collected");
}
public static void main(String args[]){
GarbageDemo gb1=new GarbageDemo();
GarbageDemo gb2=new GarbageDemo();
 g1=null;
g2=null;
System.gc();
 }
}

Output:

Object is garbage collected
Object is garbage collected

System class:
System class contains 'gc' method for requesting JVM to run garbage collector .
System.gc();

By using Runtime class:

A java application can communicate with JVM by using Runtime Object. we can get runtime
object as fallows

Runtime r=Runtime.getRuntime();

Once we create run time object we can apply fallowing methods

freememory():

 returns the free memory available in the loop

totalMemory():

returns help size
                                   

                              



No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...