2

I was wondering what is the maximum capacity of string builder (or stringbuffer)

I have a static variable in my Android app and it is supposed to hold log string. I the maximum it is holding is 130 lines and about 10000 character. I if i append more to it, but it just does not show (no error no exception)

So I was wondering if there is some sort of limitation placed in string builder or imposed by android on static variables length?

Thank you very much

Snake
  • 14,228
  • 27
  • 117
  • 250
  • Useful link: http://stackoverflow.com/questions/1179983/how-many-characters-can-a-java-string-have – Fran Verona Sep 08 '12 at 16:07
  • 1
    How are you attempting to "show" this string -- "just does not show" is not especially helpful. – CommonsWare Sep 08 '12 at 16:10
  • Why are you logging to a StringBuilder? Do you plan on periodically flushing that out to a file or a DB? (Or did you mean "long" instead of "log"?) – Cᴏʀʏ Sep 08 '12 at 16:12
  • Oh when I said show it means by writing to a file or by "inspecting" it in eclipse in debug mode – Snake Sep 08 '12 at 16:21
  • @Cory correct I am flushing it to a file a when crash happens – Snake Sep 08 '12 at 16:21
  • @Snake: maybe showing the code of where you write it out could give us some clues. Are you closing the stream you're opening to write the file before you look at it? – Cᴏʀʏ Sep 08 '12 at 16:23
  • The code is just appending to a string builder. I when I put a break point and try to inspect the stream then why do I only see 130 lines. This is even before I write to a file – Snake Sep 08 '12 at 16:29

2 Answers2

0

The java StringBuilder maximum capacity is huge and on Android it is 16mbyte.

The StringBuilder should automatically increase its capacity when you append to it but you can force it to allocate extra space using the ensureCapacity() method.

myStringBuilder.ensureCapacity(int minimumCapacity)

So, as a test, you can set it to a larger capacity and determine if the capacity is the problem or not.

Visual Micro
  • 1,561
  • 13
  • 26
0

It's not limitation of StringBuilder, but limitation of the widget you are showing the text on! For example if you are showing the long text on a TextView, then you should consider using this line:

textView.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
            263000) }) // or any other large number that is enough for you

I read somewhere that this problem is specific to some devices only.

yrajabi
  • 637
  • 1
  • 9
  • 28
  • I think that's the problem. I am not using widget but that prompted me to check on eclipse debug configuration and apperently the debugger doesn't show more than 130 lines roughly – Snake Sep 08 '12 at 16:55
  • So as long as you don't want to show it on screen, and you are sure that it is here completely (by checking size of String), so I think there is no problem at all. – yrajabi Sep 08 '12 at 17:53