JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Garbage Collection

Garbage Collector
:-
  • The Garbage Collector runs in a thread in the JVM.
  • When the free memory drops under a threshold the GC begins to run to eliminate unneeded objects
  •  Exactly when it runs and for how long, however, cannot be controlled by the user program.

  • GC is unpredictable in nature, and in fact, causes serious problems for Real Time (RT)
    applications of Java.
  • The JVM does not actually require a GC and in some special situations, the GC is
    simply turned off or left out. 
  • In that case, the program must itself make sure that it does not run out of memory.
What Does a GC Perform?

  • If a section of memory earlier used by an object is no longer referenced by any variable,
    then the Garbage Collector (GC) will release this memory for re-use by new objects. 
  • This takes place automatically without any input from the user program.
  • There are no heap commands such malloc() and free() functions in C needed to allocate
    or de-allocate memory buffers.
  • Java does use a similar "new" operator as in C++ for creating objects but has no "delete" operator.

finalize() method

  • A class can override the finalize() method and use it to do clean up tasks
    before the object is reclaimed by the GC.
  • However, there is no way to know when the finalizer will be called and if a lot of objects
    with finalizers are waiting for their final invocation, a lot of memory might be used up.
  • Generally, it is recommended to avoid application of the finalize() method and instead
    create a clean up method that you call explicitly when the object is no longer needed.
Example:-

import java.util.Date;

public class gc1 {

  @Override
  protected void finalize() throws Throwable {
    System.out.println("hello");

  }

  public static void main(String[] args) {
    Runtime rt = Runtime.getRuntime();

    System.out.println("free memory is " + rt.freeMemory());

    Date d[] new Date[10000];

    for (int i = 0; i < d.length; i++) {
      d[inew Date();
      d[inull;
    }
    System.out.println("now free memory is " + rt.freeMemory());

    System.gc();
    System.out.println("free memory after garbage collection is  "
        + rt.freeMemory());

  }

}
Output:-
java gc1
free memory is 4947536
now free memory is 4652776
free memory after garbage collection is  5007024
Download code

Example:--

class {
  private String name;

  public B(String s) {
    name = s;
    System.out.println("const..."+name);
  }

  protected void finalize() {
    System.out.println("finl.."+name);
  }
  
  int aaa(int a){
       int ar[]=new int[3];
      
       
    if(a>0)
      return 22;
    else 
      return 2;    
  }
}

class {
  public static void m() {
    B x1 = new B("X"), y1 = new B("Y");
    System.out.println(x1.aaa(2));
    
  }

  public static void main(String[] args) {
    m();
    System.gc();
  }
}
Output:-

java E
const...X
const...Y
22
finl..Y
finl..X
Download code