Sunday, 21 September 2014

How to Make an Object is eligible for Garbage Collection Explicitly

An object is said to be eligible for Garbage Collection, If it is doesn't contain any reference.
If an object is contain any reference then it is not eligible for Garbage Collection.

If an object is no longer required then assign null to all its reference , then automatically that object eligible for Garbage Collection.

ex:-                Student s1= new Student();
              here Student object is not eligible for Garbage Collection. Because s1 reference is there.
if we assign the null value for the object then that object is eligible for G.C.
           
          ex:-        s1=null;

By Requesting the JVM to Run Garbage Collector:

When ever we are making an object eligible for garbage Collector  it may not be destroyed by G.C immediately when ever jvm runs G.C then only that will be destroyed.

we can request jvm to run garbage collector, programmatically......

1) by System class:
System contain a static method G.C for this
                   System.gc();
2) By Runtime class
by using runtime object  a java application can communicate with jvm.
Runtime class is a singleton class hence we con't create Runtime object by using constructor.
we can create the Runtime object by using factory method getRuntime
                  Runtime r= Runtime.getRuntime();
       

No comments:

Post a Comment