File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Any tips on how to reduce a String's char[] length? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Any tips on how to reduce a String Watch "Any tips on how to reduce a String New topic
Author

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

Marvin Diggler
Greenhorn

Joined: Aug 30, 2005
Posts: 9
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

Joined: Jul 04, 2005
Posts: 18
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

Joined: Aug 10, 2005
Posts: 685
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

Joined: Sep 19, 2004
Posts: 531
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

Joined: Jan 30, 2000
Posts: 18671
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)..


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Any tips on how to reduce a String's char[] length?
 
Similar Threads
String index out of range exception
another NullPointerException problem
Printing repeated Characters
Null teminating a string
parseInt for using a blank line for sentinel