• Post Reply Bookmark Topic Watch Topic
  • New Topic

Update information on String Pool  RSS feed

 
Ranch Hand
Posts: 71
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the information on this web page should be updated. It says that

Unlike most objects, String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection.

I think this was true before Java 7. In Java 7, the String Pool was relocated to the heap. According to Java 7 release notes,

interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application.

This implies that all strings in the String Pool are eligible for garbage collection if there are no references to them in your program.

Also, in this code,
it says that 1 object will be eligible for garbage collection. I think that the answer should be 2 (from Java 7 upwards).
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tess,

I don't think so. The fact that interned Strings are allocated on the heap, does not mean that they won't be referenced forever by the data structure that interns them. Think of it this way:
Strings that are interned will be referenced during the entire program.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garbage collection questions are always interesting and sometimes frustrating. But that journal page was written in reference to the SCJP exam (as it was then called), where such questions are often answered. I'm no expert in these exams but I think I recall reading here in these forums that the exams no longer ask garbage-collection questions about String objects.

If that's the case then the question is less interesting to those who are focused on getting their certifications. But still an interesting question in its own right.
 
Tess Jacobs
Ranch Hand
Posts: 71
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks for clearing that up.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tess Jacobs wrote:
interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application.
This implies that all strings in the String Pool are eligible for garbage collection if there are no references to them in your program.

Or any program or class that your program references.

I'm also not quite sure what the term "interned" refers to here. If it refers specifically to String.intern(), then it's perfectly possible that literals are still permanent, but TBH, I suspect it comes under the heading of DontSweatIt.

Java's been around for quite a while now, so I tend (maybe lazily) to assume that the designers know what they're doing. String "interning" is, after all, simply an implementation decision - the language would work perfectly happily without it - so I tend to ignore things I have no control over.

As I say, probably lazy; but it gives my old brain less to agonise about.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!