Compact Strings in Java
Java 9 introduced a concept of Compact Strings to adopt a more space-efficient internal representation for strings.
Why? It aims to the reduce the size of String objects, consequently which reduces the overall footprint of Java applications.
The earlier implementation of the String class stores characters in a char array, using two bytes for each character. But most of the String objects contain only ISO-8859-1/LATIN-1 characters. Such characters require only one byte of storage, hence half of the space in the internal char arrays of such String objects is going unused.
So from Java 9, the internal representation of the String class changed from a UTF-16 char array to a byte array plus an encoding-flag field. The new String class will store characters encoded either as ISO-8859-1/Latin-1 or as UTF-16, based upon the contents of the string. The encoding flag will indicate which encoding is used. Thus half of the space is saved.
Before Java 9
// char array private final char value[]; // constructor public String() { this.value = new char[0]; } public int length() { return value.length; }
Memory footprint from Java 8 environment for 360 characters.
From Java 9
// byte array private final byte[] value; // constructor public String() { this.value = "".value; this.coder = "".coder; } public int length() { return value.length >> coder(); } byte coder() { return COMPACT_STRINGS ? coder : UTF16; }
Memory footprint from Java 11 environment for 360 characters.
If you observe the footprints from both Java 8 and 11 environment, You'll notice the difference in memory consumed by 8 for 360 characters is almost the double compared to 11.
Note: If the applications extensively use UTF-16, then you should consider disabling Compact Strings feature for a better performance.
References
More Links
Thanks for reading. Please like & share the article.