directory

  • How are strings stored in memory?
  • String.intern () method
  • How does a String cause a memory leak?
  • conclusion

Today to talk to you about our peacetime code, the most common String String code, some of its underlying principles, as well as improper use of memory leakage may cause the problem, I believe that for everyone daily development code will have certain help.

How are strings stored in memory?

First of all, when we write a String in our code, do you know how the String is stored in memory? For example, a line of code like this: String username = “zhangsan”, this “zhangsan” is actually a String, in fact, it is stored in an array, and the array size is exactly equal to the length of the String, it is immutable, as shown in the following figure.

For Java strings, there is a constant pool concept. This means that the same string content is represented in the same array in memory, rather than creating different arrays for the same string content.

String username = "zhangsan"; 
String nickname = "zhangsan";
Copy the code

Username and nickname above both point to “zhangsan” and are actually stored in the same array at the bottom, as shown in the following figure.

(system.out. println(username == nickname)) {return true for username == nickname; Because they both point to the same underlying array.

If we create a String object from a String, it must be a different object in memory, as shown in the following code:

String username = "zhangsan"; 
String nickname = new String("zhangsan"); 
System.out.println(username == nickname);
Copy the code

** return true for username and nickname Username = “username”, nickname = “nickname”, nickname = “nickname”, nickname = “nickname”, nickname = “nickname”, nickname = “nickname”, nickname = “nickname”, nickname = “nickname”, nickname = “nickname”

How is this “zhangsan” String stored inside this String? In fact, this String contains a “zhangsan” String that references the previous array, as shown below.

String.intern () method

So, if you use the string.intern () method, you’ll see that you can get the “zhangsan” String in the String, and then compare it with the String, and still return true.

String username = "zhangsan"; 
String nickname = new String("zhangsan");
System.out.println(username == nickname.intern()); // Returns true
Copy the code

How does a String cause a memory leak?

Ok, so now that you understand the basic principles of strings in Java, we can show you how we can cause memory leaks when we write code using strings. Substring () is a String interception action that causes a memory leak.

In previous versions of Java 6, when you called String.substring() to intercept a String, the underlying way it worked was that it simply copied your array of strings and marked it with an offset pointer and count. To indicate what you need after the truncated string, as shown below.

But under this mode of operation has a problem, is you every time the substring will make a copy of the original array, but for the string you only need to part of the inside, and you make a copy of the original string every time, led to a string of don’t need a copy of that part of the content is wasted, The red circles in the following figure are not required for substrings.

So what’s the problem with the red circle that’s not needed in the substring still taking up memory? This is a typical memory leak, that is, if you do a lot of operations like substring, you may copy a lot of string arrays, and then a lot of the copied string arrays will not be needed, and will take up a lot of memory. This is called a memory leak.

A memory leak is when you take up a lot of memory, and as a result you don’t use it and no one else does, it’s classic dog in the manger behavior.

Substring () was refactored from the beginning of the Java 7 version. Every time you execute String.substring, you just copy the part of the String array that you need. This avoids copying the original array of strings every time, as shown below.

conclusion

In Java 7 and later versions, the substring problem has been completely solved. However, you should be careful not to use older versions of Java to cause this problem.

Of course, now generally use Java 8 version above, especially with Java 9, Java 10 and even Java 11 these several new versions, but do not exclude some companies very old systems in the maintenance of the time, or used to be very popular Java 6 version, we in the maintenance of this kind of old system, Be careful about substring memory leaks.

END

= = = = = = = = surprise