Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String or String[] ??

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've created a midlet which seems to run out of runtime memory . I have a lot of String[] variables, which I imagine take up a fair bit of memory.
I was wondering if it would use less runtime memory if I stored the data in a String[]. E.g.,
String[] names = {Alan, Bob, Clint};
or in a delimited String. E.g.,
String namesList = "Alan,Bob,Clint,";
(and I would obviously create an extra method to extract the required element from namesList).
So does anyone know if one would be particularly advantageous than the other with regards to runtime memory?
Thanks,
James
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
Welcome to JavaRanch!
We don't have too many rules here at the Ranch, but we do have our naming policy which requires that you use a first and last name. The last name can't be an initial. Please head over here and fix your display name, pronto. Thanks, pardner. Hope to see you around the Ranch!
As to your question: storing the data as one string would definitely use a lot less memory. The String array itself is an object, and the individual Strings are each objects; because there's some overhead for storing each object, the array version has a lot more overhead. Of course, you'll need to use memory while parsing the data back out.
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks!
 
Christian Wolf
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much better than using Strings is, according to a famous book here ;-), using StringBuffers. They are not as memory consuming as String objects. But their usage is limited. If you need to do a lot of work with the strings (cut, search), then the multiple conversions from StringBuffer to String and vice versa will cost both memory and CPU cycles.
Christian Wolf
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic