2

I have a question. I was reading in a blog about substring.Quoting it below.

One point which is worth remembering here is that substring is also backed up by character array, which is used by original String. This can be dangerous if original string object is very large and substring is very small, because even a small fraction can hold reference of complete array and prevents it from being garbage collected even if there is no other reference for that particular String

I am not able to understand what does it mean to say a small reference can prevent from garbage collection. Can someone explain this concept? Thanks

benz
  • 4,561
  • 7
  • 37
  • 68
  • I believe this should answer your question: [Will a substring of another string prevent the parent string from being garbage collected?](http://stackoverflow.com/questions/15857883/will-a-substring-of-another-string-prevent-the-parent-string-from-being-garbage) – paulsm4 Jul 24 '13 at 19:19
  • 2
    FYI, this is no longer true in Oracle Java 7 as the backing `char[]` is no longer shared. – Sotirios Delimanolis Jul 24 '13 at 19:19
  • You can refer to this answer http://stackoverflow.com/questions/2514349/lost-string-garbage-collection. – André Jul 24 '13 at 19:22
  • Sotirios can you explain. Why in java 7 it is removed – benz Jul 24 '13 at 19:24
  • @benz You can look at my comment in Oscar's answer. It was removed in part for the reasons in your quote. – Sotirios Delimanolis Jul 24 '13 at 19:24

2 Answers2

3

It means that all new strings created as a result of a substring() operation hold a reference to the character array in the original string, and the original character array won't be garbage collected until all the substrings derived from it are also garbage collected, no matter if they're big or small.

As pointed out in the comments, this situation doesn't arise in recent versions of Java (1.7.0_06 and newer). See this link for an explanation, but in short: nowadays substring() creates a new string each time, so the underlying char[] doesn't get shared anymore.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

I believe this link answers your question:

Will a substring of another string prevent the parent string from being garbage collected?

// Example 1
String samplel = "ToBeGarbageCollected";
String sample2 = samplel.substring(0, 1);

// Example 2
String samplel = "ToBeGarbageCollected";
String sample2 = new String(samplel.substring(0, 1));

The substring method doesn't put a reference to the original String in the new String. What it actually does is save a reference to the original String's backing array; i.e the array that holds the characters.

... The original String's entire backing array will be remain reachable ... and that means it won't be a candidate for garbage collection.

But there is another complication. You set sample1 to a String literal, and the String object that represents a String literal is always reachable (unless the entire class gets unloaded!)

In your second example, copying the substring causes a new String to be created. And it is guaranteed that the new String won't share the backing array with the original String and the temporary substring

ALSO:

As Sotirios Delimanolis correctly says: this is no longer true of Java 7 or higher:

Java 7 String - substring complexity

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190