Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Eligible objects for garbage collectors

Mon Aug 24, 2009 5:11 pm

Eligible Objects For Garbage Collectors:
An example for you to understand when an object becomes eligible for garbage collectors. The following code snippet show you the number of object that is ready for java garbage collector .You need to keep in mind that JVM is the only controller when GC runs . Object become eligible when no of program thread can reach it (No reference to it).

Code:
public class JavaGC {

    public   static void main(String args[])
    {
        TestGC obj1 = new TestGC();
        TestGC obj2 = new TestGC();
        TestGC obj3 = obj1.dumpIt(obj2);
        obj1 = null;
        // What are the eligible objects here for GC . 
// Am here .
        
    
}
}

class TestGC 
{
    Long id;
    TestGC dumpIt(TestGC testGC)
    {
        testGC = null ; 
        return testGC 
;
    }
}
 


A t the line [b]“// Am here “ there are two object ready for GC . [/b]
1. The object of obj1.
2. The wrapper object inside the obj1 which is Long .

You can define a finalize() function for the class TestGC , this function is called once for every eligible instance before GC delete it .



Post a reply
  Related Posts  to : Eligible objects for garbage collectors
 Using Web Objects in XML (WOX) to serialize Java objects XML     -  
 What Is Garbage Collection?     -  
 garbage collection work in java     -  
 Can an object be garbage collected and it is still reachable     -  
 Get all objects for an entity     -  
 Smart Objects in photoshop     -  
 what is object's lock and which objects have locks ?     -  
 queue of objects keep track of the front and rear     -  
 invalid argument on IE with script to center objects     -  

Topic Tags

Java Basics