user1464139
user1464139

Reputation:

Garbage collection of referenced objects in Java

I have the following code:

class Test {
   public static void main(String[] args) {
      String a,b,c;
      a = new String(args[0]);
      b = a;
      a = null;
      b = null;
   }
}

Can someone tell me when a will be eligible for garbage collection. I think it's after b is made null because don't a and b reference the same object ?

Upvotes: 1

Views: 142

Answers (5)

Sephallia
Sephallia

Reputation: 396

Pitching my answer too. As the other answers say, the String/ object is available for Garbage Collection once it is no longer accessible (you no longer have a handle to it).

So if you had a one-direction linked list... [1] -> [2] -> [3] and you had a handle to [1] (which has a handle to [2] and onwards). If you set your handle to [1] to null, you would put the entire list available to the Garbage collector. As this answer says, you are able to call System.gc() to request the Garbage Collector run, but it is not guaranteed that it will.

I believe the main focus to this answer is that objects are available to the garbage collector when they are inaccessible and that does not necessarily mean that there are no references to it. In my above example, even though [1] had a handle to [2], [2] was available for the garbage collector because there was no handle to [1].

Upvotes: 1

DarthVader
DarthVader

Reputation: 55032

class Test {
   public static void main(String[] args) {
      String a,b,c; // you dont have objects yet
      a = new String(args[0]); // here u make a new object
      b = a; // b references to the same object as a
      a = null; // a points to null, reference is removed.
      b = null; // b point to null, reference is removed
   }
}

in your program the object that was created and assigned to a and b will be garbage collected after references are removed.

Garbage collector will clean up the object that has no roots and not being referenced by anything.

Upvotes: 0

Luca Geretti
Luca Geretti

Reputation: 10286

You must reason in terms of allocated objects: the String you allocated is not referenced by anyone as soon as you nullify both a and b. From thereon the garbage collector is entitled to do its job.

Upvotes: 0

thecoop
thecoop

Reputation: 46098

What do you mean by a? a is a reference, and only objects are garbage collected.

The string referenced by a, and then b, is eligible for garbage collection when nothing references it anymore. In this case, this is after both references to the string (a and b) have been changed to reference something else; in this case, null.

Upvotes: 0

cklab
cklab

Reputation: 3771

The object you've created, new String(args[0]);, will be eligible for collection once there are no longer any references to it. So let's step through the code:

a = new String(args[0]);

a points to your String, it is not eligible for collection.

b = a;

a and b point to your String, not eligible.

a = null;

b points to your String, not eligible.

b = null;

No references to your String, Garbage Colelctor is happy!

Upvotes: 1

Related Questions