File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Ranch Office and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » This Site » Ranch Office
Reply Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

Harveen Batra
Greenhorn

Joined: Nov 14, 2000
Posts: 5
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

Joined: Sep 29, 2000
Posts: 8521
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).


"JavaRanch, where the deer and the Certified play" - David O'Meara
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
Yup, exactly.


"I'm not back." - Bill Harding, Twister
 
IntelliJ Java IDE
 
subject: garbage collection
 
Threads others viewed
garbage collection
a doubt in Garbage Collection
Doubt in GC
GC doubt
Doubt in Garbage Collection
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com