Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

substring (String class) - Memory Leak (Java)

 
Steven Rodeo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Apologies if this question has been beaten to submission :-). I saw few online articles suggesting that the subString (method of String Class) returns a String backed by the same char[] array that the original String is built on ?.

Honestly, I was looking at the internal implementation of String (jdk7) and seems like this is not the case ?.

Thanks Much!
_SM.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It used to. I think it just got changed in Java 7.

It probably stemmed from the early days of Java when object creation and GC was a lot more expensive than it is now. Probably most substring() calls lead to strings that are not a tiny portion of a huge string and/or are relatively short-lived, so the cases where the "leak" would matter were pretty rare, while the memory management overhead savings were comparatively large, thus making it worth it.

Now, presumably, since the overhead associated with object creation and GC is much diminished, the designers probably figured it's no longer a worthwhile savings.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this was resolved in 7u6.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic