• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Garbage Collection  RSS feed

 
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
 
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
 
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!