Profile Image
vishnupriya

What is Java Compact Strings: -XX:-CompactStrings?

What is Java Compact Strings: -XX:-CompactStrings? Have you used this JVM argument before? What are the pros & cons of using this Java argument? Can you share your perspective/experience in using this JVM argument?

  • jvm-argument

  • xx-compactstrings

  • x-compactstrings

  • Java Compact Strings

Please Sign In or to post your comment or answer

Profile Image

Pavel Khodakovsky

Usage:


Available from Java 9.


• -XX:-CompactStrings
• -XX:+CompactStrings

 

Description:


The -XX:-CompactStrings flag disables the use of the Compact Strings feature
in the JVM.


In Java, characters and strings have always been implicitly double-byte, i.e. each
character is represented by two bytes of data in Unicode UTF-16 encoding. This
allows Java programs to handle text in languages such as Chinese, Japanese
and Korean with little additional programmer effort. However it has proven to
be wasteful of memory when a program deals predominantly with single-byte
characters such as those used in English.


In Java 9 the JVM was modified to internally store strings and characters in singlebyte form when those strings contain only single-byte characters, i.e. characters
that can be represented as a single byte using the ISO-8859-1 or Latin-1 encoding.
Note that if a string contains at least one multibyte character then it is represented
entirely as UTF-16.


This internal representation is invisible to the application programmer, and it
does not affect Java source code or existing compiled Java code. All related APIs
such as StringBuilder still treat character data as double-byte. However it can
save significant amounts of memory, especially in an application that processes
text data.


If an application overwhelmingly uses multi-byte characters (e.g. an application
with a Japanese-language user interface) then it can turn off Compact Strings
using the -XX:-CompactStrings flag. This may save the performance overhead
of the JVM checking each string for the presence of multibyte characters.


The -XX:+CompactStrings flag re-enables the use of Compact Strings. Where
both -XX:-CompactStrings and -XX:+CompactStrings have been used on a
command line, the last one takes precedence.

 

Default Value:


Compact Strings are enabled by default.


Errors:


None.

Arguments Related to -XX:-CompactStrings:


• TODO: link to -XX:UseCompressedOops
• TODO: link to -XX:UseCompressedClassPointers


Related Posts:


• Compact Strings in Java 9 (DZone Article)


NOTE:


If you have additional comments, interesting experiences or even point of disagreement with this JVM argument description, please leave a comment. Your
insights will help the entire 10+ million java developer community to develop
one standard source of documentation for all the JVM arguments.

Got something else on mind? Post Your Question

Not the answer you're looking for? Browse other questions tagged
  • jvm-argument

  • xx-compactstrings

  • x-compactstrings

  • Java Compact Strings