Java String.substring Interesting Find (31 posts)

Topic tags: java
  • Profile picture of sbook sbook259p said 4 months ago:

    I’m tracking down a memory leak at the moment and just came across this really interesting article on how Strings are implemented in a number of different JVM’s. I haven’t come across this before, but its sure nice to know about.. Scary stuff.

    http://eyalsch.wordpress.com/2009/10/27/stringleaks/

    TL;DR: The backing char arrays of Strings are held on to and can’t be garbage collected, so…

    Do this:

    String string = "abcdefghijklmnopqrstuvqxyz";
    string = new String(string.substring(2, 5));
    

    Instead of this:

    String string = "abcdefghijklmnopqrstuvqxyz";
    string = string.substring(2, 5);
    
  • Profile picture of glaucomardano glaucomardano252p said 4 months ago:

    Hmm. Nice to know this. Btw I have to assume that I hate the first way xD . I neer instantiate Boolean or Strings at all xD .

  • Profile picture of sbook sbook259p said 4 months ago:

    Yeah I always think it looks funny seeing new String() but stress testing some server software and seeing your heap usage skyrocket is worse IMO

  • Profile picture of pspeed pspeed815p said 4 months ago:

    In that example, it should make no difference as the strings are literals and will be interned anyway as part of the Class literals.

    It’s when you get a string from another source (say reading a file) and then do a substring() on it that you worry about it keeping that whole file around in memory.

  • Profile picture of sbook sbook259p said 4 months ago:

    @pspeed said:In that example, it should make no difference as the strings are literals and will be interned anyway as part of the Class literals.

    Yeah for that example it wouldn’t really matter.. I didn’t even think of that, I just consciously decided not to write out all of the code to open and read a file from memory :)

  • Profile picture of pspeed pspeed815p said 4 months ago:

    Sure. I just wanted to point out that in your example you actually waste memory instead of save it. :)

  • Profile picture of normen normen1290p said 4 months ago:

    Interesting indeed, its like doing a substring() on a string causes the returned string to reference the main string and thus it will only be released when the substring object is released. Easy to remember but you really gotta know this one ^^

  • Profile picture of pspeed pspeed815p said 4 months ago:

    Many times, this memory sharing is actually a really good thing… and it makes substring a nearly free op.

  • Profile picture of Empire Phoenix Empire Phoenix157p said 4 months ago:

    Also if you have string and want all substrings taht ar possible the additional memory cost is quite low. I agree however, that if the original string is rather large, or only a very small part of it is needed the new String approach should be used. (I think about xml processing and Nodenames for example here ^^ , btw how is the ogre loader written as we speak of it?)

  • Profile picture of shirkit shirkit20p said 3 months, 4 weeks ago:

    I’ve came across this bug last year, as my software I work with cuting string, puting strings inside another, replacing etc. and found this annoying bug, that until today wasn’t fixed.

  • Profile picture of normen normen1290p said 3 months, 4 weeks ago:

    Its not a bug, its a feature ^^ It saves you from stuffing new objects in the heap all the time.

  • Profile picture of shirkit shirkit20p said 3 months, 4 weeks ago:

    Yeah, but a feature that causes memory leak for me sounds like a bug.

  • Profile picture of sbook sbook259p said 3 months, 4 weeks ago:

    @shirkit said:
    Yeah, but a feature that causes memory leak for me sounds like a bug.

    By that logic any language without automatic garbage collection should be killed off… oO ?

  • Profile picture of shirkit shirkit20p said 3 months, 4 weeks ago:

    I consider it annoying as I spent about 2 hours to discover this thing. So, for me, it’s annoying

  • Profile picture of sbook sbook259p said 3 months, 4 weeks ago:

    @shirkit said:
    I consider it annoying as I spent about 2 hours to discover this thing. So, for me, it’s annoying

    Fair enough :)