Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Garbage Collection

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go it. Thank You.

Sampath
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic