0
String s = "hello";

String literals have references in String Literal Pool and are not eligible of garbage collection, ever. So, after above line even if I say:

s=null;

String object "hello" will still be in heap as I understand. Source: https://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html

Does same holds true for strings inside String array? Suppose we have

String[] arr = {"one","two","three"};
arr=null;

Will the 3 string objects still be on heap referenced from pool? or they will be eligible for garbage collection along with array object.

mustafa1993
  • 541
  • 1
  • 5
  • 17
  • The string literal pool isn't part of the heap. See http://stackoverflow.com/questions/4918399/where-does-javas-string-constant-pool-live-the-heap-or-the-stack – khelwood Jan 27 '17 at 10:46
  • That's why I mentioned "references". According to the description in link, constant table(Literal Pool) store references to String objects residing in **heap memory**. – mustafa1993 Jan 27 '17 at 10:48

1 Answers1

4

String literals have references in String Literal Pool and are not eligible of garbage collection, ever.

Actually, that is not strictly correct ... see below.

Will the 3 string objects still be on heap referenced from pool? or they will be eligible for garbage collection along with array object.

They will not be referenced "from the pool". The references in the pool are (in effect) weak references.

They will not be eligible for garbage collection.

What is actually going to happen is that the String objects (in the string pool) that correspond to string literals in the source code will be referenced by the code that uses the literals; i.e. there are hidden references in hidden objects that the JVM knows about. These references are what the JVM uses when you (for example) assign the string literal to something ...

It is those hidden references that mean the weak references in the pool don't break, and the corresponding String objects don't get garbage collected.

Now, if the code that defines the literals was dynamically loaded, and the application manages to unload the code, then the String objects may become unreachable. If that happens, they will eventually be garbage collected,

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks. Ok they wont be garbage collected, but am still bit confused with concept of pool. So all strings in pool have this weak hidden references? How is this hidden objects related to constant table? May be am missing something if you can brief or cite some link that will be helpful. Thanks. – mustafa1993 Jan 27 '17 at 11:50
  • I didn't say there is "weak hidden reference". I said there is a hidden reference in the code, and an "(in effect) weak reference" in the string pool. What I mean is that the string pool behaves like a weak reference in the sense that objects in the pool are not strongly reachable via the pool. – Stephen C Jan 27 '17 at 23:30
  • Try reading this: http://java-performance.info/string-intern-in-java-6-7-8/ For deeper / definitive knowledge, you can read the source code. Oracle have not published any papers, etc on this (AFAIK). – Stephen C Jan 27 '17 at 23:32
  • That helped. Thanks:) – mustafa1993 Jan 28 '17 at 04:58