---
created_at: '2010-12-11T23:05:44.000Z'
title: The String Memory Gotcha (2005)
url: http://fishbowl.pastiche.org/2005/04/27/the_string_memory_gotcha/
author: DanielRibeiro
points: 45
story_text: ''
comment_text: 
num_comments: 11
story_id: 
story_title: 
story_url: 
parent_id: 
created_at_i: 1292108744
_tags:
- story
- author_DanielRibeiro
- story_1995859
objectID: '1995859'

---
[Source](https://fishbowl.pastiche.org/2005/04/27/the_string_memory_gotcha/ "Permalink to The String Memory Gotcha - The Fishbowl")

# The String Memory Gotcha - The Fishbowl

# [The Fishbowl][1]

tail -f /dev/mind > blog

# The String Memory Gotcha

April 27, 2005 4:44 PM

**<edit date="December 2010">** This blog post describes an old Java bug that has since (to the best of my knowledge) been fixed and all affected versions EOL'd. Regardless, it remains a cautionary tale about the problem of [leaky abstractions][2], and why it's important for developers to have some idea of what's going on under the hood. **</edit>**

Every Java standard library I have seen uses the same internal representation for String objects: a char[] array holding the string data, and two ints to represent the offset from the start of the array that the String starts, and the length of the String. So a String with an array of [` 't', 'h', 'e', ' ' , 'f', 'i', 's', 'h' ]`, an offset of 4 and a length of 4 would be the string "fish".

This gives the JDK a certain amount of flexibility in how it handles Strings: for example it could efficiently create a pool of constant strings backed by just a single array and a bunch of different pointers. It also leads to some potential problems.

In the String source I looked at (and I'm pretty sure this is consistent across all Java standard library implementations), all of the major String constructors do the 'safe' thing when creating a new String object - they make a copy of only that bit of the incoming char[] array that they need, and throw the rest away. This is necessary because String objects must be immutable, and if they keep hold of a char[] array that may be modified outside the string, [interesting things can happen][3].

String has, however, a package-private "quick" constructor that just takes a char array, offset and length and blats them directly into its private fields with no sanity checks, saving the time and memory overhead of array allocation/copying. One situation this constructor is used in is `String#substring()`. If you call `substring()`, you will get back a new String object containing a pointer to the _same_ char[] array as the original string, just with a new offset and length to match the chunk you were after.

As such, substring() calls are incredibly fast: you're just allocating a new object and copying a pointer and two int values into it. On the other hand, it means that if you use `substring()` to extract a small chunk from a large string and then throw the large string away, the _full data_ of that large string will continue to hang around in memory until all its substrings have been garbage-collected.

Which could mean you carrying around the complete works of Shakespeare in memory, even though all you wanted to hang on to was _"What a piece of work is man!"_

Another place this constructor is called from is StringBuffer. StringBuffer also stores its internal state as a char[] array and an integer length, so when you call `StringBuffer#toString()`, it sneaks those values directly into the String that is produced. To maintain the String's immutability, a flag is set so that any subsequent operation on the _StringBuffer_ causes it to regenerate its internal array.

(This makes sense because the most common case is `toString()` being the last thing called on a StringBuffer before it is thrown away, so most of the time you save the cost of regenerating the array.)

The potential problem again lies in the size of the char[] array being passed around. The size of the array isn't bound by the size of the String represented by the buffer, but by the buffer's _capacity_. So if you initialise your StringBuffer to have a large capacity, then any String generated from it will _occupy memory according to that capacity_, regardless of the length of the resulting string.

How did this become relevant?

Well, some guys [at work][4] were running a profiler against [Jira][5] to work out why a particular instance was running out of memory. It turned out that the JDBC drivers of a certain commercial database vendor (who shall not be named because their license agreement probably prohibits the publishing of profiling data) were consistently producing strings that contained _arrays of 32,768 characters_, regardless of the length of the string being represented.

Our assumption is that because 32k is the largest size these drivers comfortably support for character data, they allocate a StringBuffer of that size, pour the data into it, and then `toString()` it to send it into the rest of the world.

Just to put the icing on the cake, if you have data larger than 32k characters, you overload the StringBuffer. When a StringBuffer overloads, it automatically doubles its capacity. 

As a result, every single String retrieved from the database takes up _some multiple of_ 64KB of memory (Java uses two-byte Unicode characters internally), most of it empty, wasted bytes.

Ouch.

The first computer I owned had 64KB of memory, and almost half of that was read-only. Which means every String object coming out of that driver is at least twice the size of a game of [Wizball][6].

This turned out to be false. According to [a reddit comment][7]: "The c64 used bank switching to allow for a full 64KB of RAM and still provide for ROM and memory-mapped I/O."

One possible workaround is that the constructor `new String(String s)` seems to "do the right thing", trimming down the internal array to the right size during the construction. So all you have to do is make an immediate copy of every String you get from the drivers, and make the artificially bloated string garbage as soon as possible.

Still, ouch.

_Previously:_ [Competition in a Nutshell][8]

_Next:_ [Apple: What's Next?][9]

![Atom Feed][10]

**The Fishbowl is the Weblog of Charles Miller.** ([Archives][11])

[cmiller@pastiche.org][12]

[Flickr][13] | [Facebook][14] | [Twitter][15]

[1]: https://fishbowl.pastiche.org
[2]: http://www.joelonsoftware.com/articles/LeakyAbstractions.html
[3]: https://fishbowl.pastiche.org/2003/01/13/when_is_a_constant_not_a_constant
[4]: http://www.atlassian.com
[5]: http://www.atlassian.com/software/jira
[6]: http://marticus.midnightrealm.org/wiz/
[7]: http://www.reddit.com/r/programming/comments/ekhx1/the_string_memory_gotcha/c18rtyd
[8]: https://fishbowl.pastiche.org/2005/04/26/competition_in_a_nutshell/
[9]: https://fishbowl.pastiche.org/2005/05/04/apple_whats_next/
[10]: https://fishbowl.pastiche.org/pictures/feed-icon-14x14.png
[11]: https://fishbowl.pastiche.org/archives/
[12]: mailto:cmiller%40pastiche.org
[13]: http://flickr.com/photos/cmiller
[14]: http://facebook.com/carlfish
[15]: http://twitter.com/carlfish

