File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Garbage collection of non-heap strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage collection of non-heap strings" Watch "Garbage collection of non-heap strings" New topic
Author

Garbage collection of non-heap strings

Harish Verma
Ranch Hand

Joined: Feb 27, 2006
Posts: 32
If a string were created without using the new keyword as follows:

String s = "abcd";

This string "abcd" will be placed in the special Java string pool. My question is whether this pool is subject to garbage collection or not. If it is, is there any difference in the way garbage is collected from standard heap and this special pool? I think this pool is not a target for collection, can someone pls confirm this? Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

First, let me clear up a common misconception: the "String pool" is not a special area of memory where Strings are allocated; it's basically a hash table where references to Strings are kept. The Strings themselves, like all other Strings, are in the heap.

Now, will such Strings ever be garbage collected? The JLS is silent on this question. I've been told that in some implementations, they can be, but only if the class that defined them itself collected. This will only happen if the ClassLoader that loaded the class is collected; and this happens only with user-created class loaders, so it's not something you'll come across in a beginner situation.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection of non-heap strings