• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collection  RSS feed

 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After what line will the object referenced by i become eligible for garbage collection in the following code?

public class Pannel{
public static void main(String argv[]){
Pannel p = new Pannel();
p.go();
}
public void go(){
Integer i = new Integer(10);
Integer i2 = i;
i = null;
Integer i3 = new Integer(0);
i2= null;

}
}

I think the answer is after the line i = null;
But it is i2 = null;

when i = null is it not eligible for GC.

Please explain

Thanks
Sampath
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thats true...



public class Pannel{
public static void main(String argv[]){
Pannel p = new Pannel();
p.go();
}


public void go(){

Integer i = new Integer(10); // i is pointing to the Object

Integer i2 = i; //i2 is also pointing to the object.So 2 references //i and i2 for this object now..

i = null; //i is dereferenced and only i2 remains pointing to the
//object.So only 1 reference remains.

Integer i3 = new Integer(0);

i2= null; //Here i2 is also dereferenced...and the object is not //having any more references..

}
}



Regards
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go it. Thank You.

Sampath
 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you look carefuly at the code, i2 gets refered to i, so when you nulify i, i2 will still be reffering to it.

I hope i explained myself correctly.

Regards,
Marzo.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!