Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Any tips on how to reduce a String's char[] length?

 
Marvin Diggler
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have heard that when you create a string and then shorten it, the original char[] length is maintained and wastes memory. Is there a simply way to shorten the length of the char[] after shortening the String length?
 
Tom Henner
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable. If you change one it really is creating a new one behind the scenes.

String a = "123456789" ;
int l = a.length();
int l2 = a.toCharArray().length ;
System.out.println( "" + l + " " + l2 ) ; //9 9
a = "12345678" ;
l = a.length();
l2 = a.toCharArray().length ;
a = "12345678" ;
System.out.println( "" + l + " " + l2) ; //8 8
 
Norm Radder
Ranch Hand
Posts: 732
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you give an example of how/where you think memory would be wasted?
Most likely the garbage collector will reclaim unreferenced objects.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Older versions of the JVM had a memory leak when calling delete on the whole StringBuffer and reusing the same instance over again. Strings cannot be resized so I doubt that's an issue.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable, but there are numerous methods which create new Strings based on existing Strings. One particular example is substring(). Prior to JDK 5.0, if you used substring() on an existing String, you'd get a String using the exact same char[] backing array as the original. I once had a long file to read in which each line was a record, and I needed to extract the record ID (the first part of the line, roughly a tenth of the total length) and save it in a HashSet. The problem was, this used ten times as much memory as it should have - because each record ID was a String made from substring() which referenced a char[] which contianed the entirey of each line which had been read - even though 9/10 of that line was data I didn't need or care about. The solution at the time was to use a new String (String) constructor to make a fresh new copy of each ID string - one that did not have any reference to the previous char[] array. This made it possible to discard all the unnecessary data in the original char[] arrays.

As of JDK 5.0, it looks like all the methods in String (and StringBuffer) which previously used shared char[] buffers behind the scenes, have apparently been rewritten to always construct a fresh new char[] with no references to the old data. I think the end result is that the new methods are a bit less efficient, but also a bit safer (in the sense that it's less likely there will be excess memory used in non-obvious places)..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic