4

I have an application that stores 3 MB in the String pool, which a considerable amount of memory for this particular application. I want to manage all the String by myself, avoiding them to go to the PermGen. I know that Java 7 puts the interned Strings on the heap, but unfortunately Java 7 isn't an option right now.

There is a way to disable the JVM String pool on Java 6, or at least a way to make the String literals don't go to there?

Daniel Pereira
  • 2,720
  • 2
  • 28
  • 40
  • 1
    Possible [duplicate](http://stackoverflow.com/questions/5672688/can-we-avoid-interning-of-strings-in-java) – jlordo Nov 20 '12 at 13:12
  • I'm not sure if that is what it looks like, and whether it is supported with your Java versions/platform, but you may want to research on the [`-XX:+UseStringCache` non-standard option](http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html). *Update*: see http://stackoverflow.com/q/1049983/21567 as well. – Christian.K Nov 20 '12 at 13:19
  • 1
    You don't have to use Strings. You could use char[] or an own implementation of "CharSequence". I have the experience, that the normal internal java-handling is really efficient, and you have to take a huge effort to get in to the same region. – Jordi Laforge Nov 20 '12 at 13:22
  • @JordiLaforge my problem with the intern is that I have a large amount of Strings that don't go to the String pool and match to the ones that are there. I can't use the intern method to put all of them in the String pool because there are also many String that are unique in the code and will be collected in short notice, but are created in the same location that the ones that could be internalized. – Daniel Pereira Nov 20 '12 at 13:28
  • So maybe this discussion helps you: http://stackoverflow.com/questions/5238131/how-can-i-ensure-the-destruction-of-a-string-object-in-java I can only think of "destoying" unneeded Strings. (Boahhh, sounds really against the language.). – Jordi Laforge Nov 20 '12 at 13:40
  • 1
    And maybe one JVM-Switch helps you too: -XX:+UseCompressedStrings – Jordi Laforge Nov 20 '12 at 14:07
  • 3 MB of memory costs about 0.1 cents and is worth about 0.5 seconds of your time (assuming you are paid minimum wage) It doesn't sound "considerable" to me. ;) – Peter Lawrey Nov 20 '12 at 14:15
  • @Peter Lawrey I'm working on a project that is porting an application from Delphi. This application was made many years ago, to fullfill the lack of client-server application on the company . Basically saying, this application raise as many instances as is necessary to allow multiple users. So, 3 MB can turn into a considerable amount memory when there are many users using it at the same time. If we consider that the Java application is using almost 4 times more memory, it definately starts to be something to worry about. – Daniel Pereira Nov 20 '12 at 14:42
  • So if you have 1000 users using 3 MB each that's worth almost 30 minutes of your time. It doesn't make sense to run one JVM per user, but I am assuming its not worth trying to fix. – Peter Lawrey Nov 20 '12 at 14:47
  • 1
    @PeterLawrey It is not up to me this decision :) – Daniel Pereira Nov 20 '12 at 15:06

1 Answers1

2

According to jlordo's link - you cannot disable it. However, if you want to avoid interning, you can load strings from file - this way they will not be interned, unless you explicitly say so.

popfalushi
  • 1,332
  • 9
  • 15