• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings, StringBuffers and available memory

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic