• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

garbage collection

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class testfloat
{
public static void main(String []arf){
A a = new A();// line one object
A b = new A();// line 2 object
b = a;// after this line is object created at line 2 eligible for gc ???
a = null;
}
}
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anil,
Yes the object created at line 2 may be garbage collected, since there are no active references to this object.
regards
vadiraj
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friend
in the given coding the refrence variable at line 2 is not claimed for garbage collection. because it can be reachable through the refrence varible store in the refrence variable b.
if you write the coding in the following manner it will be eligible for GC.
class testfloat{
public static void main(String args[]){
A a = new A();
A b = new A();
a = null;
}
}
Thanks.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vishad, I don't agree with you. The object that b references in line 2 doesn't have any references once you say that b=a. So that object can be gc'd after you assign b to a. The object that a references is still not eligble for gc after you set it to null because b is now looking at that item also.
Bill
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anil,
the object created at line 2 will b eligible for GC, as u have asigned a new reference to object b, so now there is no reference to the object which has been created at line 2. This is the reason that it will b eligible for GC.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic