Memory leakage of the substring method in String

Source: Internet
Author: User

Memory leakage of the substring method in String

As we all know, in JDK, the substring method in the String class has a memory leakage problem. The reason is that it was previously because JDK 1.7 and later versions have been fixed, I have said that JDK1.6 also has this problem, but I checked the source code 1.6 installed on my local machine and saw that there was no memory leakage problem. I also read the source code 1.7, like my local 1.6, is my 1.6 version actually 1.7 ?! Alas, no matter what it is, version 1.7 certainly does not have this problem (version 1.5 and older versions certainly have it), so you can rest assured to use it.

The cause of Memory leakage is that in the original version, substring is implemented as follows:

   public String substring(int beginIndex, int endIndex) {if (beginIndex < 0) {    throw new StringIndexOutOfBoundsException(beginIndex);}if (endIndex > count) {    throw new StringIndexOutOfBoundsException(endIndex);}if (beginIndex > endIndex) {    throw new StringIndexOutOfBoundsException(endIndex - beginIndex);}return ((beginIndex == 0) && (endIndex == count)) ? this :    new String(offset + beginIndex, endIndex - beginIndex, value);    }

The String constructor used here is as follows:

    // Package private constructor which shares value array for speed.    String(int offset, int count, char value[]) {this.value = value;this.offset = offset;this.count = count;    }

This. value = value is a problem because there are several private member variables in the String class:

 /** The value is used for character storage. */    private final char value[];    /** The offset is the first index of the storage that is used. */    private final int offset;    /** The count is the number of characters in the String. */    private final int count;    /** Cache the hash code for the string */    private int hash; // Default to 0

See, this implementation still references the value [] of the original string variable, and returns a string that looks like "truncated" through offset and count, as a result, the JVM considers that this initial string is still being referenced and does not support gc. However, the reason for this is that the JDK compiler of SUN (oracle acquired in 10 years) is also that, efficiency issues, as mentioned in the Notes:

Package private constructor which shares value array for speed.

The consequence is that if there is a very large and long string, I only need a small part of the string to be implemented using substring, if the seemingly "new" short string has not been recycled by JVM, it is equivalent to the original large string, in particular, you pay the short "new" string directly as a reference to a static global variable. In addition, if the number of accesses is large, the "price" should be quite impressive, but it can be as simple as this new (s. substring () to avoid this problem.

The reason why substring does not exist in the new JDK is that the constructor is changed to this:

  public String(char value[], int offset, int count) {        if (offset < 0) {            throw new StringIndexOutOfBoundsException(offset);        }        if (count < 0) {            throw new StringIndexOutOfBoundsException(count);        }        // Note: offset or count might be near -1>>>1.        if (offset > value.length - count) {            throw new StringIndexOutOfBoundsException(offset + count);        }        this.offset = 0;        this.count = count;        this.value = Arrays.copyOfRange(value, offset, offset+count);    }

Among them, the value variable is no longer referenced, but a new one is created, so there is no such problem. Do you have a new understanding of this method after reading it?

Reproduced please note-Author: Java my life (Chen Lei xing) Original Source: http://blog.csdn.net/chenleixing/article/details/43646255


Finally, the netizens who have read this carefully, if they feel that I have something wrong with this programmer or that you have a good idea

SubmitSuggestions or ideasYou have provided your valuable words (messages) to you, me, andProgrammers are fasterLocal growth and progress .......


Zookeeper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.