This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java Micro Edition and the fly likes Strings, StringBuffers and available memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Strings, StringBuffers and available memory" Watch "Strings, StringBuffers and available memory" New topic
Author

Strings, StringBuffers and available memory

James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
I thought I would share this. I have seen a lot of advice going around saying that it is better to use comma-delimited Strings rather than String[]'s, that it is better to concatenate Strings by creating a temporary StringBuffer and .append() to extension String...
But I've run some tests and have found the following:
Strings use less memory than StringBuffers.
String[]'s use less memory than comma-delimited Strings.
It's better (in terms of memory) to concatenate with '+' than concat() or converting to StringBuffer and using append().

For my tests I created a number of midlets that created very large arrays of Strings or StringBuffers, and experimented with the various types of concatenation.
After all the String/StringBuffer work was done, I did a Runtime.getRuntime().gc() and wrote out the available heap memory with Runtime.getRuntime().freeMemory().
I wonder why my results suggest to do the opposite of what seems to be recommended with Strings/StringBuffers?
a sanjuan
Ranch Hand

Joined: Jun 12, 2002
Posts: 164
if you could publish your code and others could run it, then perhaps we'll get to the bottom of this. i'm also wondering about your phrase about converting string to stringbuffer. on the face of it, a single stringbuffer used instead of the multiple objects created by concatenating string objects should be more efficient.
[ May 06, 2004: Message edited by: a sanjuan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings, StringBuffers and available memory