Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

First, a quick introduction to strings

An extremely high frequency data structure used in everyday work. Used to represent any object that requires a character description. Take the information on an ID card as an example. Name, gender, nationality, address, and ID number are all strings. By extension, in the real life around people, most attributes of all objects will use the string as a national data structure.

Then, the creation and storage of string objects

We know that creating a String can be either a String assignment or a new declaration. As shown below:

Here is the concept of string constant pools, which are data maintained to reduce the number of strings created by the JVM and to improve the performance of queries for data. The string constant pool is stored in different locations within the JVM in different JDK versions. The following table is the specific storage location information:

So here’s the question.

First, why adjust the position of the string constant pool after 1.7?

THE reason is that before 1.7, THE default Space of permanent generation is small and GC frequency is low. When a large number of strings cannot be recycled, FullGC is easy to produce STOP THE WORLD or OOM: PermGen Space.

After 1.7, the string constant pool is stored in the heap, where there is a lot of memory for storing strings, and the heap is divided into young generation and old generation, with high GC frequency.

Second, why is byte[] storage changed after 1.9?

According to statistics from many applications, String is the main component of heap usage, and most String objects contain only Latin characters, which require only one byte of storage space. Therefore, using char[] is a waste of space for some scenarios. After optimization to byte[], encoding flag bits are introduced to indicate whether utF-16 encoding (two bytes for characters) or Latin-1 encoding (a single byte for characters) are used. In this way, it not only saves memory space, but also supports the storage of diversified data.

Next, the bottom of the string constant pool

The String Pool uses a fixed-size HashTable structure. The default size of a String Pool varies according to JDK versions. You can run the -xx :StringTablesize command to set the length of a String Pool.

Furthermore, the efficiency of string concatenation is discussed.

Using the “+” sign will default (after JDK1.5) to generating a StringBuilder object and going to the Append () method (before JDK1.5 it was StringBuffer). Take the following figure as an example.

Javac stringSeries. Java, javap stringSeries. javap StringSeries

The corresponding entry to creating a StringBuilder object and executing elementary () is shown in the following figure:

A small summary

In summary, when there is a lot of String concatenation, it is recommended to use StringBuilder to declare objects and append rather than String +, which creates objects frequently and increases memory overhead. In the next installment, we’ll take a closer look at other public methods provided by String.