• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

garbage collection  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When is variable 'a' likely to be garbage collected
class A
{
static String a="Hello";
static String b="there";
public static void main(String args[])
{
a="afsfa";
a=null;
a=b;
}
}
My doubt is whether the likely candidate for garbage collection is at line 1 or line2.no doubt setting a object reference to null makes it a candidate for garbage collection yet String is also immutable so when we assign a new value to a a new object is created which is passed a reference and this should i think be the first candidate for garbage collection?
pls remove me out of this anamoly at to which is the first candiadate for garbage collection.
e-mail at harveen@onebox.com

------------------
 
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a is set to afsfa the string "Hello" is available for garbage collection, however a is not because it has be re-set to a different address. When a is set to null it would be food for the gc(), except that the future reference protects it.
Since this is a question about Java instead of about JavaRanch, I expect that this question is going to get moved, very shortly. You will probably find it in Java in General (Beginner).
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, exactly.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!