Quick revision
The short answer
Before JDK 7u6, a String was a window (an offset and a count) onto a char array, so substring() was O(1) and simply shared the parent's array. That made a tiny substring of a huge string keep the whole array alive, which caused real memory leaks. Since 7u6, substring() copies just the characters it needs, so it costs O(n) in the length of the substring but never holds on to the parent.
What to remember
In this answer, we saw that the old String was an offset and a count into a shared char array, so substring() was O(1) but could keep an enormous array alive through a tiny result. JDK 7u6 removed offset and count, made substring() copy only the characters it needs at a cost linear in the substring's length, and made every String about 8 bytes smaller. When we need cheap views today, we use CharSequence-based code, CharBuffer.wrap, or index-taking methods, and we call toString() before storing anything long term.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Before JDK 7u6, a String was a window (an offset and a count) onto a char array, so substring() was O(1) and simply shared the parent's array. That made a tiny substring of a huge string keep the whole array alive, which caused real memory leaks. Since 7u6, substring() copies just the characters it needs, so it costs O(n) in the length of the substring but never holds on to the parent.
Why this question is asked
This question looks like trivia, but it teaches two lessons that matter in real work: sharing memory to save time can quietly create leaks, and the JDK is free to change how a class works inside as long as its public behaviour stays the same. Interviewers at the ten-year level ask it to see whether we understand object layout and reachability, not just method names. In this answer, we will look at how the old String was laid out, reproduce the leak it caused, see what changed in 7u6 and what it cost, and finish with the options we have today when we really want cheap views into a large piece of text.
1A Quick Recap of How the Old String Was Built
Before JDK 7 update 6, a String object held four fields: value, a reference to a char array; offset, the index in that array where this string starts; count, how many chars belong to it; and hash, the cached hash code. The characters themselves lived in the array, not in the String object.
Because of offset and count, many String objects could point into the same array, each seeing a different slice of it. That is exactly what substring() did. It did not copy anything. It created a new String with the same array, a larger offset and a smaller count.
So taking a substring was constant time, whatever its length. That sounds like a pure win, and for short-lived strings it often was.
2Reproducing the Leak
The catch is reachability. The garbage collector frees an object only when nothing reachable points to it. The small substring points to the parent's array, so as long as the substring lives, the whole array lives, even if the parent String object itself is long gone.
Here is the typical case. A program reads a large file line by line, or a whole file into one String, and keeps only a short token from each one in a map.
| Before 7u6 | 7u6 and later | |
|---|---|---|
| Fields of String | value, offset, count, hash | value, hash (plus coder since JDK 9) |
What substring() does | new String sharing the parent's array | copies the chosen characters |
Cost of substring() | O(1) | O(k), k = substring length |
| Parent array kept alive? | yes, while the substring lives | no |
| Size of a String object | about 32 bytes | about 24 bytes |
Notice that nothing in this code looks wrong. The leak is invisible in the source; we only see it in a heap dump, where a handful of 8-char strings retain huge arrays. The usual workaround in those days was new String(line.substring(0, 8)), which forced a copy. Developers had to know about the trap to avoid it.
3What Changed in 7u6 and Why
In JDK 7 update 6, the JDK developers removed the offset and count fields. Every String now owns its own array, and substring() copies the range it needs into a new one. The reason was that the leak was common, hard to diagnose and surprising, while the speed of O(1) substring mattered less than people assumed: most substrings are short, and copying a few dozen chars is cheap.
There was a second benefit. Two int fields fewer means every String object is smaller. On a typical 64-bit JVM with compressed references the object shrank from about 32 bytes to about 24. Applications hold millions of strings, so 8 bytes per String adds up to real memory across the whole heap, for every program, without any code change.
So the cost today is linear in the length of the result, not in the length of the parent. Taking 8 characters from a 10 MB string copies 8 characters. Since JDK 9 and compact strings, the copy is of bytes rather than chars, and for Latin-1 text it is one byte per character, so the copy is even smaller.
4When We Really Want Views Without Copying
Sometimes we do want the old behaviour, for example when a parser walks through a large buffer and looks at thousands of small fields it never keeps. Copying each field just to compare or parse it is wasted work. The answer is not to bring back shared Strings but to work with CharSequence, the interface that String, StringBuilder and CharBuffer all implement, and to pass indexes around instead of cutting new strings.
Notice that CharBuffer.wrap gives the same trade-off the old String had: the view keeps the whole text reachable. That is fine for a short-lived view inside a parsing loop, and it is exactly the thing to avoid in a long-lived cache. When we store something, we call toString() on the view, which makes a small independent copy. Methods that accept a CharSequence with start and end indexes, such as Integer.parseInt(CharSequence, int, int, int) and StringBuilder.append(CharSequence, int, int), let us avoid the temporary strings completely.
5Follow-up questions
Was String.subSequence also changed?
Yes, indirectly. String.subSequence simply calls substring, so it copies too, even though its return type is CharSequence. If we want a real view, we have to create one explicitly with CharBuffer.wrap(text, start, end) or write our own small CharSequence that holds the parent and two indexes.
Can the copying substring itself cause memory problems?
It can cost allocation if we take millions of substrings in a hot loop, because each one is a new object and a new array. That shows up as allocation rate and young-generation GC work, not as a leak. The fix is to avoid creating the strings at all, by comparing regions or parsing in place, rather than to share arrays.
Why did nobody keep both behaviours, one copying and one sharing?
Because the sharing version cannot be made safe automatically. The JDK cannot know whether a substring will be short-lived or stored forever. A single predictable rule, always copy, is easier to reason about, and the people who need views can ask for them explicitly with CharBuffer or their own CharSequence.
6Conclusion
In this answer, we saw that the old String was an offset and a count into a shared char array, so substring() was O(1) but could keep an enormous array alive through a tiny result. JDK 7u6 removed offset and count, made substring() copy only the characters it needs at a cost linear in the substring's length, and made every String about 8 bytes smaller. When we need cheap views today, we use CharSequence-based code, CharBuffer.wrap, or index-taking methods, and we call toString() before storing anything long term.
Open with
Before Java 7 update 6, a String was a char array plus an offset and a count, and substring just created a new String pointing into the same array with a different offset and count.
Then, if they want more
So it was constant time, but it had a nasty side effect: a tiny substring kept the entire parent array reachable. The classic case was reading a large file, taking a short key out of each line and putting it in a map.
The keys looked tiny, but each one retained a whole line, and you only found it in a heap dump. People worked around it with new String of the substring to force a copy. In 7u6 the JDK removed offset and count, so each String owns its own array and substring copies the characters it needs.
That makes substring linear in the length of the result, not of the parent, which for typical short substrings is cheap, and it cannot leak the parent anymore. As a bonus, every String object got about 8 bytes smaller, which matters when a heap holds millions of strings.
If I really need views without copying, for example in a parser, I write the code against CharSequence and pass indexes, use CharBuffer.wrap with a start and end, or use methods like Integer.parseInt that take a range. I just make sure I call toString before storing anything, so a long-lived object never holds the whole buffer.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 6, Java 7, Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Before JDK 7u6, String.substring shared the parent's char array. Why was that changed...": https://scrivio-interview-library.netlify.app/java/strings/8362924d40/