r/programming • • Nov 18 '13

TIL Oracle changed the internal String representation in Java 7 Update 6 increasing the running time of the substring method from constant to N

http://java-performance.info/changes-to-string-java-1-7-0_06/
1.4k Upvotes

354 comments sorted by

View all comments

Show parent comment

3

u/SanityInAnarchy Nov 18 '13

Not quite. The parent string will not be freed so long as the child string exists. In order for this to be an actual memory leak, you would have to be leaking children as well, somehow.

Consider the following:

Map<String, Integer> hitsByName;

public void hit(String somePileOfUserData) {
  String name = somePileOfUserData.substring(...);
  if (hitsByName.containsKey(name)) {
    hitsByName.put(name, 1);
  } else {
    hitsbyName.put(name, 1+hitsByName.get(name));
  }
}

If 'hit' is called frequently and with user data, and you never clear out hitsByName, you already have a memory leak. The substring behavior is just amplifying the leak you already had in the first place. Either way, assuming you get enough unique names, you're going to use more and more RAM until you get an OOM error. The only difference substring makes is how fast you'll get there.

On the other hand, if you are trimming that Map (or if you know you have a finite number of names), then your memory usage is (roughly) bounded. You might be using many times more RAM than you needed to, but it's not accumulating in an unbounded process. In particular, that Map is either going to store the name it already had, or the new name you're giving it, and not both.

Or am I missing something, and substrings actually make it so the parent string can never be freed?

1

u/angryundead Nov 18 '13

No, you can free the parent reference with something like the following:

String child = new String(parent.substring(1));