Question subject: Eligible objects for garbage collectors
Posted: Mon Aug 24, 2009 5:11 pm
Joined: Tue Mar 27, 2007 10:55 pm Posts: 2103 Location: Earth Has thanked: 39 time Have thanks: 56 time
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 .
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )