Domanda

i an not able to track where garbage collector being invoked.plz help

class Garbage01
{ 
public static void main(String args[]) 
{
    Garbage01 h = new Garbage01(); 
    h.methodA(); /* Line 6 */
} 
Object methodA() 
{
    Object obj1 = new Object(); 
    Object [] obj2 = new Object[1]; 
    obj2[0] = obj1; 
    obj1 = null; 
    return obj2[0]; 
} 
}
È stato utile?

Soluzione

Garbage collection in java is called automatically and it collects object which are eligible for garbage collection.

Object becomes eligible for Garbage collection or GC if its not reachable from any live threads or any static refrences in other words you can say that an object becomes eligible for garbage collection if its all references are null.

Cyclic dependencies are not counted as reference so if Object A has reference of object B and object B has reference of Object A and they don't have any other live reference then both Objects A and B will be eligible for Garbage collection.

So there is no possibility to check when garbage collector is called/invoked.

Altri suggerimenti

Garbage collector never invoked in methodA()

because Garbage collection takes place after the method has returned its reference to the object. The method returns to line 6, there is no reference to store the return value. so garbage collection takes place after line 6.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top