Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings, StringBuffers and available memory

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic