Reputation: 31212
Consider the following scenario:
String dude = "Harold";
//some stuff
dude = "Kumar";
Once dude
is assigned its second value of "Kumar", a whole new object separate from "Harold" comes into existance. What I am wondering is, since there is clearly no use for "Harold" any more, is it garbage collected instantly or at some later time when the JVM deems it appropriate?
UPDATE:
Just because dude
has been set to a new value (object) does not necessarily mean all reference to the old object have been eliminated. If another reference invokes dude
after "Harold" and before "Kumar", it will obviously retain the "Harold" object:
String dude = "Harold";
//some stuff
String interim = dude;
dude = "Kumar";
System.out.println("dude = " + dude);
System.out.println("interim = " + interim);
prints
dude = Kumar
interim = Harold
So there are more considerations than I originally envisioned therefore the initial value doesn't necessarily have to be out of scope after the change.
Upvotes: 0
Views: 1350
Reputation: 3161
since there is clearly no use for "Harold" any more
How have you come to this conclusion? Just by looking at the single class where you have made the reference? But according to the Java Language Specification, string literals are reused in the following cases
So unless you are sure that all none of the above are true, you probably can't expect it to be even eligible for garbage collection.
Upvotes: 2
Reputation: 13374
While pretty much every current implementation of the Java spec uses a tracing garbage collector, I don't believe the specification requires that the heap be managed using a tracing GC.
One can imagine GC implementation incorporates reference counting. In this variation to the case above (engineered to avoid the const pool detail):
String dude = new String("Harold");
//
dude = new String("Kumar");
The original object referenced by dude
's ref count goes to zero, and could be garbage collected immediately.
The net point being -- with Java, there is no guarantee, but usually you don't have to worry about it.
Also, an old reference that talks about Java GC.
Upvotes: 2
Reputation: 43728
Lets forget, that constant String
may be handled differently. The main reason why the object is not immediately garbage collected is that there may be other references to it. For example if your "some stuff" contains a statement like
t = dude;
The object is not unreachable when you assign a new value to dude
. To find out all references is rather costly and therefore only done when necessary.
Upvotes: 1
Reputation: 24192
Nothing is garbage-collected instantly, and trying to force garbage collection is discouraged (and becomes increasibgly difficult with each java release).
Yes, the minute "Harold" is no longer reachable (no chain of pointers leads to it) it becomes eligible for garbage collection and may be collected (if the GC ever runs). having said that, Strings may be allocated in the const pool, which resides in the perm gen and is not GC'ed* (at least up until java 7). more info on interning strings here: http://www.codeinstructions.com/2009/01/busting-javalangstringintern-myths.html
* - not entirely accurate, the const pool is GC'ed even though its in perm gen. its complicated.
Upvotes: 5
Reputation: 8301
This one: "at some later time when the JVM deems it appropriate". Garbage Collection costs CPU and can mean pausing threads. GCs are hideously complicated things that constantly balance CPU vs memory usage.
As others have said, Strings are more complicated, but I assume you just wanted to know in general, for java objects. I don't think all this waffle about String is really relevant to your question and is making this Question far more complicated than it needs to be!
Upvotes: 2
Reputation: 4380
Normally garbage collection doesn't happen immediately. Furthermore, Strings are immutable and are kept in a global table (by most JVMs as far as I know) so it might never be collected.
Upvotes: 2
Reputation: 41200
String
is immutable
. When you reassign variable dude dude = "Kumar"
older string object get out of scope.
And when Garbage Collector start working, it collected that object.
Upvotes: 0