File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Can we delete a String Object from JVM String Pool? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we delete a String Object from JVM String Pool?" Watch "Can we delete a String Object from JVM String Pool?" New topic
Author

Can we delete a String Object from JVM String Pool?

Aakash Parashar
Ranch Hand

Joined: Jul 25, 2005
Posts: 122

Can we delete a String Object from JVM String Pool?


Your's Aakash
SCJP 1.4 96%, SCBCD 1.3 94%, http://java-application-programming.blogspot.in
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Nope. If you don't want a String to be in the String pool, you must make sure it doesn't get put into it in the first place: don't use it as a String literal, and don't intern() it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Aakash Parashar
Ranch Hand

Joined: Jul 25, 2005
Posts: 122

Rob Spoor wrote:Nope. If you don't want a String to be in the String pool, you must make sure it doesn't get put into it in the first place: don't use it as a String literal, and don't intern() it.


We are using some java libraries which only return String, and we want to delete those String once used, because they are too large to be occupied in memory, causing OutOfMemoryError.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Are you sure they are the reason for the memory error? String literals don't take up much memory, unless they are either very very long or very very numerous.

Are you even sure they are String literals, and not non-interned Strings? These should be garbage collected as usual.

Part of your problem may be that you are still holding a reference to them, or perhaps to their internal char[]. When you call substring on a String, that substring shares the original String's char[]. If the substring is much smaller than the original, that is a waste of memory. This is one of the few cases where using copying the String makes sense. So if this is the case, use the following to create substrings:
This will create a new String that does not share the char[], and therefore this char[] can be garbage collected too.
Aakash Parashar
Ranch Hand

Joined: Jul 25, 2005
Posts: 122

Problem solved by reducing the number of instances of Spring Objects. Used StringBuffer and CharacterSequence in place of String where ever it was possible.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Why StringBuffer rather than StringBuilder?
Aakash Parashar
Ranch Hand

Joined: Jul 25, 2005
Posts: 122

StringBuilder will be preferable when you use java 5 or higher version.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we delete a String Object from JVM String Pool?