Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

Before JDK 7u6, String.substring shared the parent's char array. Why was that changed, and what does substring cost today?

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.

Java
// Roughly what JDK 6 did inside String
public String substring(int begin, int end) {
return new String(offset + begin, end - begin, value); // same char[] as the parent
}
String line = "ORDER-123;CUSTOMER-9;..."; // imagine this is 10 MB long
String id = line.substring(0, 9); // "ORDER-123", O(1), no copy

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.

Java
Map<String, Integer> counts = new HashMap<>();
for (String line : Files.readAllLines(bigFile)) { // each line about 1 MB of chars
String key = line.substring(0, 8); // JDK 6: shares the 1 MB array
counts.merge(key, 1, Integer::sum);
}
// JDK 6: 10,000 keys kept 10,000 arrays alive, about 20 GB of char data for 160 KB of keys
// JDK 7u6 and later: 10,000 small copies, about 160 KB of char data
The same substring before and after 7u6
Before 7u67u6 and later
Fields of Stringvalue, offset, count, hashvalue, hash (plus coder since JDK 9)
What substring() doesnew String sharing the parent's arraycopies the chosen characters
Cost of substring()O(1)O(k), k = substring length
Parent array kept alive?yes, while the substring livesno
Size of a String objectabout 32 bytesabout 24 bytes
Sizes are typical for a 64-bit HotSpot JVM with compressed references; check your JVM's layout.

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.

Java
// JDK 7u6 and later, in spirit
public String substring(int begin, int end) {
int len = end - begin;
if (begin == 0 && end == length()) return this; // whole string: no copy
return newString(value, begin, len); // copies len characters
}
String s = "abc";
System.out.println(s.substring(0) == s); // true: the full range returns the same object
System.out.println(s.substring(1)); // bc, a new String with its own array

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.

Java
String text = loadHugeText();
CharSequence view = CharBuffer.wrap(text, 1000, 1010); // read-only view, no copy of the chars
System.out.println(view.length()); // 10
int n = Integer.parseInt(text, 3, 8, 10); // Java 9+: parses chars 3..7 in place
boolean same = text.regionMatches(1000, "ORDER", 0, 5); // compares a region, no substring
StringBuilder out = new StringBuilder();
out.append(text, 1000, 1010); // appends a range without a temporary String

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.

What to say in the interviewAbout 100 seconds aloud

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.
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/