wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 121

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: 19651
    
  18

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: 121

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: 19651
    
  18

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: 121

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: 37950
    
  22
Why StringBuffer rather than StringBuilder?
Aakash Parashar
Ranch Hand

Joined: Jul 25, 2005
Posts: 121

StringBuilder will be preferable when you use java 5 or higher version.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can we delete a String Object from JVM String Pool?
 
Similar Threads
String Pool and GC
String objects
GC- String
Do something with String in non-pool memory
Instances of string objects