JVM -XX:+StringCache argument?
Asked Answered
C

6

20

I was recently reading about all the JVM arguments available in JRE 6 [Java VM Options] and saw this :

-XX:+StringCache : Enables caching of commonly allocated strings.

Now I was always under the impression that Java kept a pool of interned (correct word?) Strings and when doing something like String concatenation with literals it was not creating new objects, but pulling them from this pool. Has anyone ever used this argument, or can explain why it would be needed?

EDIT: I attempted to run a benchmark, to see if this argument had any effect and was unable to get the Sun JVM to recognize it. This was with:

java version "1.6.0_11"  
Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode,
sharing)

So I'm not sure if this argument works at all.

Cystectomy answered 26/6, 2009 at 16:14 Comment(6)
I think most string operations don't use interning. The interning is rather used by the JVM to store string values from the class files and avoid excessive memory consumption because of duplicates.Lidialidice
Great question. The sun docs are very unclear here. However, docs on interning are clear that it only operates under specific circumstances (string literals) so perhaps the JVM arg applies this behavior in a wider set of circumstances.Vogue
I can't find this option in the OpenJDK source. What do you suppose that means?Ha
@mmyers: It is probably not supported in OpenJDK. Sun states: "Options that are specified with -XX are not stable and are not recommended for casual use. These options are subject to change without notice."Tremendous
Perhaps a benchmark is in order?Cystectomy
In my job we've fond that Java 5 cannot be trusted to intern all strings it could (when we started hitting OutOfMemory exceptions). We had to add calls to .intern() here and there. Maybe this option solves this problem in Java 6?Betook
C
0

I have not been able to find a single JVM that even accepts this supposed argument - so I guess there's not much else to say.

Cystectomy answered 29/9, 2009 at 20:15 Comment(2)
Oracle JRocket 6 ;) docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/…Risorgimento
You need -XX:+AggressiveOpts to enable many of the weird options, -XX:+UseStringCache including. Here is ref: mail.openjdk.java.net/pipermail/jdk7-dev/2011-April/002024.htmlRangel
P
6

I believe when used with -XX:+AggressiveOpts it enables the same String objects to be returned when creating the String with the same text (though not through new String, of course). There is a profile phase where a cache is build up, and after a point the cache is switched to read only. It gets higher scores in certain benchmarks.

Pagandom answered 26/6, 2009 at 16:29 Comment(0)
S
3

-XX:-UseStringCache works for me, strangely.

my jdk version should be 1.6.0_22

Stilt answered 21/11, 2010 at 8:36 Comment(0)
W
1

I too couldn't get the above to work, but the latest JBB @ spec.org shows it's use: -XX:-UseStringCache. I'll have to re-run benchmarks to see if it makes a difference (an XML heavy app).

Wheeling answered 22/11, 2009 at 10:38 Comment(0)
O
1

I also have not been able to find a JVM which respects this setting; as commented the quality and thus usefulness of the documentation around JVM parameters is terrible, and yet for some reason seems to be an area where JVM vendors see room for competitive differentiation - although to be fair Oracle/Sun is by far the worst.

Anyhow if you find that your app in some particular area uses a small number of string values repeatedly then it is definitely sensible to use interning - by using the String.intern() method to return an intern-pool value. Note that you have to use the return value, this is not a side-effect on the original value.

As with all profiling/performance tweaks this needs to be done carefully with metrics and testing. It can be significant (has been for me) but if the pool of values is not small it degrades performance and you need to be aware that the pool of String values is held in the Perm Gen and so using it will affect memory usage, GC etc.

Onionskin answered 7/7, 2010 at 14:28 Comment(0)
P
1

As of JDK 8.0, this option has been removed. It is unclear to me what, if anything, can be used as a replacement.

http://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html

 -XX:+UseStringCache

    Enables caching of commonly allocated strings. This option was removed from JDK 8 
    with no replacement.
Penitential answered 25/6, 2016 at 19:48 Comment(0)
C
0

I have not been able to find a single JVM that even accepts this supposed argument - so I guess there's not much else to say.

Cystectomy answered 29/9, 2009 at 20:15 Comment(2)
Oracle JRocket 6 ;) docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/…Risorgimento
You need -XX:+AggressiveOpts to enable many of the weird options, -XX:+UseStringCache including. Here is ref: mail.openjdk.java.net/pipermail/jdk7-dev/2011-April/002024.htmlRangel

© 2022 - 2024 — McMap. All rights reserved.