• Post Reply Bookmark Topic Watch Topic
  • New Topic

garbage collection  RSS feed

 
Harveen Batra
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

------------------
 
Cindy Glass
"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).
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, exactly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!