Compact Strings in Java
Source: Internet

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.

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.

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

OpenJDK - Compact Strings

More Links

Bitsnqubits blog

Thanks for reading. Please like & share the article.

要查看或添加评论,请登录

Amith Kumara H B的更多文章

  • Generics in Java

    Generics in Java

    Introduction Generics is the most important feature and was first introduced in Java 5. It enable types (classes and…

  • Java Functional Interfaces

    Java Functional Interfaces

    Introduction Functional Interface was introduced in Java 8. It is an interface which has only one abstract method.

  • Java Method References

    Java Method References

    Introduction Lambda expressions are used to create anonymous methods. Sometimes, however, a lambda expression does…

  • Java Lambda Expressions

    Java Lambda Expressions

    Introduction ?Lambda Expression was introduced in Java 8. They provide a clear and concise way to represent one method…

  • Printing numbers in sequence using multiple threads - Java

    Printing numbers in sequence using multiple threads - Java

    Problem statement: Print numbers in sequence using multiple threads Example: Input: No of threads(n): 3, Print numbers…

  • ExecutorService vs ExecutorCompletionService in Java

    ExecutorService vs ExecutorCompletionService in Java

    Introduction Let's say there are few tasks which performs some complex operation and returns value. These tasks take…

    1 条评论
  • Java Executor Framework Tutorial With Example

    Java Executor Framework Tutorial With Example

    Introduction Java 5 has introduced a very powerful framework called Executor framework which creates and manages the…

  • Local Variable Type Inference - var in Java

    Local Variable Type Inference - var in Java

    Java 10 introduced type inference for local variables. Previously, all local variable declarations required an explicit…

    1 条评论

社区洞察

其他会员也浏览了