3

As we know when ever we are creating String object like String value = new String("ABC"); then new String object will be created and when we use value variable again as value="xyz" then a new String object will be created.

So my question is that at which point previously created String object will be garbage collected?

erhun
  • 3,549
  • 2
  • 35
  • 44
satish
  • 43
  • 1
  • 6

2 Answers2

7

Whenever you call new in JAVA it create an object in heap but in case of String literals, it will go into the String Constant Pool.

Sample code:

String value = new String("ABC");
value = "xyz";

Now in the above sample code "ABC" and "xyz" string literals will go to the String Constant Pool and will not be garbage collected but finally value is referring to "xyz" from the String Constant Pool.

So basically there are 3 objects, 2 in the String Constant Pool and 1 in the heap.

at which point previously created String object will be garbage collected?

The object is created by new will be garbage collected once its scope/life is finished or there is no reference to access it. It's applicable similarly for all the objects including String as well.

Since the value reference will be pointed to the existing object with the value "xyz" within the string constant poll in the next line, so that previously created object using new in the heap is eligible for garbage collection but not "ABC" string literal that is still in the string constant pool.


Try to visualize it.

enter image description here

Read more...

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
3

Just like other Objects for String also JVM follows same approach , once application looses its reference it is eligible for garbage collection.

For your case new String("ABC") is not interned so it will not go in String pool and when you create String for "ABC" again a new Object is created in heap.

String value  = new String("ABC");
       value  = new String("ABC");

In above example after executing both lines first object is eligible for garbage collection.

String created by value="xyz" is interned also so it will be created in String pool and when u create String using value="xyz" the previous object is returned from String pool.

String value  ="xyz";
       value  ="xyz";

In above example after executing both lines only one Object is created for which we have reference also so it is not eligible for garbage collection.


More details on when does a String go in String pool.

Yes Cases

  1. new String() always creates new Object. It is not interned so you can not take it back from memory.

No Cases

  1. String a ="aa"; if already available it retrieves from pool , when not available it creates new object which is interned also.
    1. new String().intern() or "aa".intern(); if already available it retrieves from pool , when not available it creates new object which is interned also.
    2. Concatenation ( "a" + "b" )
Vipin
  • 4,851
  • 3
  • 35
  • 65
  • 1
    Concatenation of string literals creates a pooled literal. See the JLS. – user207421 Nov 19 '17 at 10:21
  • @EJP you are right, don't remember how did I test it that time and came to this conclusion. But today's test shows you are right. I have updated answer, could not update it properly using phone. – Vipin Nov 19 '17 at 18:41