We found that string. the string returned by substring () will still save the original string. In fact, the string generated in substring is a great design to share the content array with the original string, this prevents the substring from re-copying the character array each time. This design can save a lot of memory in many cases, because these strings all reuse the original string, and only identify each string through the start and end values of the int type. In the above case, a small number of strings are intercepted from a huge string for future use. Such a design results in a large amount of redundant data.
Since the root cause of a large amount of memory usage is that string. substring () contains a large number of original strings in the returned results, an obvious way to reduce memory waste is to remove these original strings. There are many methods. Here we adopt a more intuitive method, that is, call New String again to construct a string that only contains the captured string.
String newString = new String(largeString.substring(0,2));
Java 7 Implementation
In Java 7, the implementation of substring discards the sharing mechanism of the character array of the previous content. For the sub-string (except itself), array replication is used to realize that a single string holds its own content.
--------------------- This article is from the csdn blog of the wheat field. For the full-text address, click: 77647800? Utm_source = copy
Shaanxi Diesel Engine-mechanical IP -------- QQ request huichuang