Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the new round of optimization of String strings by JDK9?

Shulou Source: shulou.com Published: 2022-06-03 04:54:05 10月17日 Update

JDK9 on the String string of the new round of optimization is what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, hope you can get something.

The String class can be said to be the most frequently used class in Java programming, and if the performance of the String string can be optimized, then the performance of the program must be greatly improved.

In addition, JDK9 has improved and upgraded the String string, which can reduce the memory of String string by half in some scenarios, thus reducing the number of GC of JVM.

The underlying storage of String

During the interview, we usually say that String strings are immutable, and we have to create new strings every time. So why are String strings immutable?

Let's take a look at the underlying storage structure of the String string:

Public final class String implements java.io.Serializable, Comparable, CharSequence {private final char value []; public String () {this.value = "" .value;} public String (String original) {this.value = original.value; this.hash = original.hash;} / /.}

What do you see? When we new a String object, the corresponding string is actually stored inside the String object as an array of char. And this char array is final, that is to say, immutable.

This is why we say that String strings are immutable. When the string changes and the char array is immutable, you can only create a new object, a new char array.

Optimization of underlying storage

In the case of JDK8 and previous versions, instead of using the char array for storing strings in JDK9,String, we use the byte array instead.

Public final class String implements java.io.Serializable, Comparable, CharSequence {@ Stable private final byte [] value; private final byte coder; @ Native static final byte LATIN1 = 0; @ Native static final byte UTF16 = 1; static final boolean COMPACT_STRINGS; public String () {this.value = ".value; this.coder =".coder } @ HotSpotIntrinsicCandidate public String (String original) {this.value = original.value; this.coder = original.coder; this.hash = original.hash;} / /...}

Not only has the char array been changed to the byte array, but also a member variable of coder has been added.

In the program, the vast majority of strings contain only English alphanumeric characters, using Latin-1 coding, each character occupies a byte. If you use char, one char takes up two byte, which takes up twice the memory space.

However, if characters such as Chinese are used in the string that are beyond the scope of the Latin-1 representation, there is no way to express them using Latin-1. At this point, JDK uses UTF-16 encoding, which takes up the same space as the old version (using char []).

The coder variable represents the encoding format, and String currently supports two encoding formats, Latin-1 and UTF-16. Latin-1 needs one byte to store, while UTF-16 needs 2 or 4 bytes to store.

It is said that this improved scheme is that JDK developers use big data and artificial intelligence to investigate the heapdump information of thousands of applications and come to the conclusion that most String is represented by Latin-1 character coding, only one byte of storage is enough, and two bytes is a complete waste.

The COMPACT_STRINGS attribute is used to control whether the compact function of String is enabled. It is on by default. You can use the-XX:-CompactStrings parameter to turn this feature off.

Benefits of improvement

The benefits of the improvement are very obvious. First of all, if most of the Latin-1 character sets are used in the project, the memory footprint is greatly reduced, and the same hardware configuration can support more business.

When the memory is reduced, it will further reduce the number of GC, and then reduce the frequency of Stop-The-World, which will also improve the performance of the system.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Character string array storage byte encoding variable memory object underlying performance program two that is function variable benefit for this case more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Docker Shulou Information MariaDB MySQL