So, if I declare a String object str and assign it a value, and then assign a different value to the same String object str, since String class is immutable, a new String object will be created in memory and str now points to this new object. BUT, since the older String object is now referenced by none, why couldn't that be garbage-collected immediately? I'm aware that if a new String literal is created with the same value, the same earlier object will be returned but it's less likely that it happens anyway. Is there a reason JVM doesn't run GC when it gets to know that the bad developer is creating 3 String objects in every iteration of his gigantic for loop?
-
_I'm aware that if a new String literal is created_ How are **you** creating and assigning your `String` objects? – Sotirios Delimanolis Nov 25 '14 at 18:17
-
I mean to say is I do String strAllOverAgain = "asd"; //assume asd was the value of earlier string – Anoop Dixith Nov 25 '14 at 18:22
-
`String foo = "asd"; foo = "asd";` does not recreate the asd string instance. It's re-using the same. And refering to "asd" does not create it, it can be a constant that exists somewhere, likely since the the class that uses it is loaded – zapl Nov 25 '14 at 18:28
-
I know. I meant this. `String foo ="asd"; System.out.println("foo is " + foo.hashCode()); foo = "def"; System.out.println("foo is " + foo.hashCode()); String boo = "asd"; System.out.println("boo is " + boo.hashCode());` – Anoop Dixith Nov 25 '14 at 18:32
-
1regarding how / when / where literals are created: http://blog.jamesdbloom.com/JVMInternals.html#constant_pool – zapl Nov 25 '14 at 18:37
-
What? I still don't understand your confusion. It's a `String` literal. It's stored in a common root classloader pool. It won't be GCed. – Sotirios Delimanolis Nov 25 '14 at 18:37
2 Answers
Because it's not worth the time. The generational collector is much faster at this problem than micro managing each String at allocation time. There's a lot of overhead in managing little bits of memory compared to the what the copying collector is doing en masses.

- 115,893
- 19
- 128
- 203
Note The below behavior has changed since java 7 to prevent the issue described below. This answers gives a good detailed explanation Java 7 String - substring complexity
In addition to what Will said, every time you create a "String" does not mean that the JVM might not have other references to that string. This is because of the "intern" behavior of String
e.g.
String x = "This is a new string"
String y = x.subString(0,1) ; //y = "T"
x = null; //discard X
However a new string literal is not created internally. Rather both 'x' and 'y' reference the same literal String. The start and end index property of "String" y would be (0,1) --> pointing at literal --> "This is a new string"
Disclaimer: The terminology might not be exactly accurate

- 1
- 1

- 1,071
- 7
- 13
-
`y` will not have a reference to the same `String` (or rather its backing `char[]`) referenced by `x`. Not since Oracle JDK 7, at least. – Sotirios Delimanolis Nov 25 '14 at 18:41
-
Yep this changed in java 7, I didn't know that. Will add a note – Saifuddin Merchant Nov 25 '14 at 18:53