5

I've seen several similar questions, but their answers don't really answer my question.

Where does an old string go when we edit string and a new one is created.

String s1 = "Hello"
s1 += "World"

We had a string Hello but then a new string is created and it is HelloWorld.

Is string Hello still in string pool(i guess no, as we lost reference to it). Is garbage collector going to destroy it?

And what if we create a new string and use intern method. Is this string going to be taken from pool?

String s1 = "Hello";
s1 += "World";

String s2 = "Hello";

In this topic, Stephen C said that

The strings will be garbage collected if they ever become unreachable

But I did such thing:

        String s1 = "Hello";
        System.out.println(System.identityHashCode(s1));
        s1 += "World";

        System.gc();

        String s2 = "Hello";
        System.out.println(System.identityHashCode(s2));

and it printed the same identity hashcode, though I lost reference to the "Hello" string and a new one should have printed another identity hash code.

He also said that

This means that the String is reachable for as long as the method could be executed.

But I did an experiment similar to the above one where the method created a string, then in another method, I created the same string(before used System.gc) and it printed out the same identity hash code.

Why doesn't GC destroy the string in my 2 cases if I lose reference to them?

THE Waterfall
  • 645
  • 6
  • 17
  • 3
    Run your test 1000 times. The GC runs when the GC runs. You can't get deterministic results. – nicomp Aug 04 '18 at 15:19
  • As far as I know, strings in the string pool do not get garbage collected. – Henry Aug 04 '18 at 15:22
  • 2
    Please don't [reask the same question](https://stackoverflow.com/questions/51641728/is-an-old-string-going-to-be-deleted-from-a-string-pool). If you don't think the duplicate(s) applies, edit your question to explain why, then wait for the reopen votes to come in. – Sotirios Delimanolis Aug 04 '18 at 15:23
  • @SotiriosDelimanolis I apologize. I created a new question, as I edit the previous one with the additional explanation and it wasn't reopened. I guess I should've waited more. – THE Waterfall Aug 04 '18 at 15:40
  • @nicomp I run it like 5000 times but GC doesn't want to do anything. Maybe I did something wrong – THE Waterfall Aug 04 '18 at 15:41
  • Didja get the same result every time? – nicomp Aug 04 '18 at 16:13
  • @nicomp Yes, absolutely. The identity hash code is the same. – THE Waterfall Aug 04 '18 at 16:15
  • 2
    The cited answer also says: “In practice, the `String` objects that correspond to string literals *typically* do not become candidates for garbage collection. This is because there is an *implicit* reference to the `String` object in the code of every method that uses the literal.” – Holger Mar 26 '21 at 17:21
  • ... which means that unless you can cause all of those methods to be unloaded, the `String` object corresponding to the literal will remain reachable, and won't be GC'd. This is the normal (i.e. "typical") situation. If you want to demonstrate a string literal being GC'd you need to jump through hoops with creating a classloader and dynamically loading classes so that those classes can be unloaded. – Stephen C Oct 18 '22 at 02:52
  • If you really want to know more, look at this answer which has demo code: https://stackoverflow.com/a/30777646/139985. But frankly, all of this knowledge about how the string pool works is of minimal practical use. The 4 things you do need to know are 1) don't use `==` to compare strings, 2) don't use `intern`, 3) **most** strings **don't** get created in the string pool, and 4) the string pool >>is<< garbage collected ... so it doesn't matter. – Stephen C Oct 18 '22 at 02:55

0 Answers0