Translate

Sunday 31 August 2014

Garbage Collector

In Java garbage collector is a low priority thread that runs automatically and destroys the objects that are not being referred to by any reference variable. We can request to run garbage collector by System.gc( ). It is not guaranteed that this request would be fulfilled.

e.g
1.)
String s1="xy";
String s2=s1;
s1=s1+z;
s2=s1;
s1=null;      //"xy" is now eligible for garbage collection
s2=null;


2.)
void Method1( )
{
    String s1 = "xy";
    String s2;
    s2 = method2(s1);
    s2 += "cd";
    s2 = null;
}      //s1 is local variable so after the end of method Method1 "xy" is eligible for garbage collection

String Method2(String s3)
{
    s3 = "Java";
    return s3;
}

No comments:

Post a Comment

Total Pageviews