• 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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"
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, exactly.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!