Garbage Collection Under Standing


Garbage Collection depth Understanding :
  1. Introduction
  2. how many way make an object garbage collection
    1. nullifying an object when it is no longer use then it will be go to garbage collector
Student s1=new Student();
Student s2=new Student();
//now no object eligible for garbage collection
………………..
s1=null;
//now s1 will be go to garbage collector
…………………..
s2=null;
//now s2 will be go to garbage collector


    1. re-assign an object to another one object the first object is go to garbage collector.
Student s1=new Student();
Student s2=new Student();
//now no object eligible for garbage collection
………………………
s1=new Student(); //So previous s1 object go to garbage collector
…………………………….
s2=s1; //So now previous s2 object go to garbage collector


    1. Object create inside a method then all object go to garbage collector after finished this method.
                                                              i.      If a method hold reference variable, where method have a reference Object return type
                                                            ii.      If a method not hold any reference variable, where method have a reference object return type.
                                                          iii.      If a method hold a global object reference variable, where method have a reference object return type.

    1. Island Of Isolation

    1. NB 1  :   So when a object have no reference then it will go to garbage collector
    2. NB 2 : If an object have  reference but if this reference is only for internal no external then it is also go to garbage collector example ILand of Isolation
  1.  How many way call to garbage collector
    1. By using System class Example System.gc(); is static method of java.lang.*
    2. By suing runtime class Example Runtime.getRuntime().gc(); is an instance method of java.lang.*;
  2. Difference between system.gc() and runtime.getRuntime().gc();
System.gc()
Runtime.getRuntime().gc();
1> Public class System{
……
public static void gc(){
Runtime.getRuntime().gc();
}
}

2>So performance is very slow
1>Runtime.getRuntime().gc();






2>So performance is very good


  1. Finalization: GC call finalize() method just before destroy an object to perform clean up activity.
protected void finalize() throws Throwable{} in Object class
·         so this method is empty implementation
  1. Case 1: finalize method call on the object basic
class  Test{
public static void main(String …args){
String t1=new String();
t1=null;  // after nullifying this object go to garbage collector
System.gc(); //garbage collector call of  t1 object basic here t1 is String object not Test //class object
System.out.println(“End of Main method”);
}

public void finalize(){
System.out.println(“Kartik destroyed”);
}
}

Out put 1:
End of Main method


Example 2
class  Test{
public static void main(String …args){
Test t1=new Test();
t1=null;  // after nullifying this object go to garbage collector
System.gc(); //garbage collector call of  t1 object basic here t1 is Test class object so it will call test class garbage collector
System.out.println(“End of Main method”);
}

public void finalize(){
System.out.println(“Kartik destroyed”);
}
}

Out put 1:
End of Main method
Kartik destroyed


Out put 2:
Kartik destroyed
End of Main method

  1. Case 2: finalize method call explicitly
Based on our requirement we can call finalize method explicitly, then it will be work as normal method call and object won’t be destroyed. But before destroying an object Garbage collector always finalize() method.

class  Test{
public static void main(String …args){
Test t1=new Test();
t1.finalize(); //this is call like as normal method of t1 object respective
t1.finalize(); //this is also call like normal method of t1 object respective
t1=null;  // after nullifying this object go to garbage collector
System.gc(); //garbage collector call of  t1 object basic
System.out.println(“End of Main method”);
}

public void finalze(){
System.out.println(“Kartik destroyed”);
}
}

Out put 1:
Kartik destroyed
Kartik destroyed
End of Main method
Kartik destroyed

Out put 2:
Kartik destroyed
Kartik destroyed
Kartik destroyed
End of Main method


  1. Finalize method call by two way
    1. By programmer
When run finalized method by programmer. If runtime get any exception then the program terminate abnormally. Then we get UnCaughtException by raising that exception

    1. By gc()
When run finalized method by JVM. If runtime get any exception then the program terminate normally.
Class Test{
 public static void main(){
    Test t=new Test();
     t.finalize();
     }
public void finalize(){
System.out.println(“Mandal”);
}
}
  1. Case 3: Finalize method call only one time for any object even though if the object call multiple time garbage collector

class  Test{
static Test s;
public static void main(String …args) throws InteruptedException{
Test t=new Test();
System.out.println(t.hashCode());
t=null;
System.gc();
Thread.sleep(5000);
System.out.println(s.hashCode());
s=null;
System.gc();
Thread.sleep(10000);
System.out.println(“End of Main method”);
}

public void finalize(){
System.out.println(“Kartik destroyed”);
s=this; //this is not give to destroy the object but second time it will delete 
}
}


Out put:
2346362
Kartik destroyed
2346362
End of Main method

  1. Case 4: Any java
  2. Case 5: Without System.gc() call in program how Garbage collector call internal System.gc() when we use low memory then gc automatic call by JVM to JVM wise
class  Test{
static int count;
public static void main(String …args) {
j=100 or 1000 or 10000 or 100000 or 1000000…..
for(int i=0;i<j;i++){
Test t=new Test();
t=null;
}
System.out.println(“End of Main method”);
}

public void finalize(){
cunt++;
System.out.println(“Kartik destroyed”+count);
}
}


Out put:
2346362
Kartik destroyed
2346362
End of Main method


  1. Memory leaks :
The object which are  not use in our program and no to call garbage collector that type of object occur memory leak.  So different type of tools like HP-J-Meter, IBM –TIVOLI, HP-PATROL, J-PROBE etc
  1. String and StringBuffer
String
StringBuffer
String s=new String(“Kartik”);
  s.concat(“Mandal”);//no assign
Syso(s);
Out put
Kartik


Once we create a String object we can’t perform any changes in the existing object. If we are trying to perform any changes with those changes a new object will be created. This non changeable nature is nothing but immutability of the string object.


String s=”Kartik”;
s=s.concat(“Mandal”);
or
s=s+”Mandal”;
or
s +=”Mandal”
So the overloaded + and += operator also meant for concatenation purpose only.

StringBuffer s=new StringBuffer (“Kartik”);
  s.append(“Mandal”);
Syso(s);
Out put
KartikMandal

Once we create a StringBuffer object we can perform any type of operation in the existing object. This changeable is nothing but mutability of StringBuffer object



Example 1 Java Garbage Collection:




Previous
Next Post »