Why does appending “” to a String save memory?

前端 未结 9 1826
粉色の甜心
粉色の甜心 2020-11-28 17:34

I used a variable with a lot of data in it, say String data. I wanted to use a small part of this string in the following way:

this.smallpart =          


        
相关标签:
9条回答
  • 2020-11-28 18:08

    In Java strings are imutable objects and once a string is created, it remains on memory until it's cleaned by the garbage colector (and this cleaning is not something you can take for granted).

    When you call the substring method, Java does not create a trully new string, but just stores a range of characters inside the original string.

    So, when you created a new string with this code:

    this.smallpart = data.substring(12, 18) + ""; 
    

    you actually created a new string when you concatenated the result with the empty string. That's why.

    0 讨论(0)
  • 2020-11-28 18:13

    I think this.smallpart kept referencing towards data, but why?

    Because Java strings consist of a char array, a start offset and a length (and a cached hashCode). Some String operations like substring() create a new String object that shares the original's char array and simply has different offset and/or length fields. This works because the char array of a String is never modified once it has been created.

    This can save memory when many substrings refer to the same basic string without replicating overlapping parts. As you have noticed, in some situations, it can keep data that's not needed anymore from being garbage collected.

    The "correct" way to fix this is the new String(String) constructor, i.e.

    this.smallpart = new String(data.substring(12,18));
    

    BTW, the overall best solution would be to avoid having very large Strings in the first place, and processing any input in smaller chunks, aa few KB at a time.

    0 讨论(0)
  • 2020-11-28 18:21

    If you look at the source of substring(int, int), you'll see that it returns:

    new String(offset + beginIndex, endIndex - beginIndex, value);
    

    where value is the original char[]. So you get a new String but with the same underlying char[].

    When you do, data.substring() + "", you get a new String with a new underlying char[].

    Actually, your use case is the only situation where you should use the String(String) constructor:

    String tiny = new String(huge.substring(12,18));
    
    0 讨论(0)
  • 2020-11-28 18:21

    Appending "" to a string will sometimes save memory.

    Let's say I have a huge string containing a whole book, one million characters.

    Then I create 20 strings containing the chapters of the book as substrings.

    Then I create 1000 strings containing all paragraphs.

    Then I create 10,000 strings containing all sentences.

    Then I create 100,000 strings containing all the words.

    I still only use 1,000,000 characters. If you add "" to each chapter, paragraph, sentence and word, you use 5,000,000 characters.

    Of course it's entirely different if you only extract one single word from the whole book, and the whole book could be garbage collected but isn't because that one word holds a reference to it.

    And it's again different if you have a one million character string and remove tabs and spaces at both ends, making say 10 calls to create a substring. The way Java works or worked avoids copying a million characters each time. There is compromise, and it's good if you know what the compromises are.

    0 讨论(0)
  • 2020-11-28 18:23

    Firstly, calling java.lang.String.substring creates new window on the original String with usage of the offset and length instead of copying the significant part of underlying array.

    If we take a closer look at the substring method we will notice a string constructor call String(int, int, char[]) and passing it whole char[] that represents the string. That means the substring will occupy as much amount of memory as the original string.

    Ok, but why + "" results in demand for less memory than without it??

    Doing a + on strings is implemented via StringBuilder.append method call. Look at the implementation of this method in AbstractStringBuilder class will tell us that it finally do arraycopy with the part we just really need (the substring).

    Any other workaround??

    this.smallpart = new String(data.substring(12,18));
    this.smallpart = data.substring(12,18).intern();
    
    0 讨论(0)
  • 2020-11-28 18:25

    Doing the following:

    data.substring(x, y) + ""
    

    creates a new (smaller) String object, and throws away the reference to the String created by substring(), thus enabling garbage collection of this.

    The important thing to realise is that substring() gives a window onto an existing String - or rather, the character array underlying the original String. Hence it will consume the same memory as the original String. This can be advantageous in some circumstances, but problematic if you want to get a substring and dispose of the original String (as you've found out).

    Take a look at the substring() method in the JDK String source for more info.

    EDIT: To answer your supplementary question, constructing a new String from the substring will reduce your memory consumption, provided you bin any references to the original String.

    NOTE (Jan 2013). The above behaviour has changed in Java 7u6. The flyweight pattern is no longer used and substring() will work as you would expect.

    0 讨论(0)
提交回复
热议问题