33

I know this may be a really noobish qustion to ask but I found no good reliable resource where I can get this info.

In an Android application, I'm trying to hold data in a String by just continuing to append it with more data . And finally when the user closes the application or hits a "save button", I log it into a data .csv file.

I want to know how much data can a simple String hold in java?, So that the app does not crash if the String datatype runs out of the memory allocated. BTW I have tried to stress test my application, and it seems to run fine no matter how much data I store in one string.

Adit

Adit Gupta
  • 863
  • 3
  • 8
  • 23

3 Answers3

39

Seeing as the String class' length() method returns an int value, the maximum length that would be returned by the method would be Integer.MAX_VALUE, which is 2^31 - 1 (or approximately 2 billion.)

So you can have a String of 2,147,483,647 characters, theoretically. I don't think you should need much more than that.

However, as @TedHopp has pointed out in the comments and posted in his answer, the Android system limits your heap space, going as low as 16 MB. Therefore, you'll never practically be able to reach the theoretical limit, and will max out with a String somewhere in the 4-64 million character range.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Good luck getting anywhere near that on an Android device. OP is asking for practical limits ("so that the app does not crash"), not theoretical limits imposed by the Java language. – Ted Hopp Mar 12 '13 at 18:39
  • @TedHopp Right you are. I was answering based on the title saying "Java". I'll edit my answer – Raghav Sood Mar 12 '13 at 18:40
  • Thanks man, Also StringBuilder usage was a nice idea as well, but I dont think I would require more that 4 million chars in my application! – Adit Gupta Mar 12 '13 at 18:48
  • that impossible 2 Billion ? w O.o . – Oussaki Aug 11 '14 at 19:28
  • 2 GB string should normally be enough :) if you have that much ram for an individual string. – 99Sono Jun 24 '20 at 12:15
10

This will probably be limited by the heap size available to the application. This can vary with both the phone model and the carrier/phone producer. For Android devices, the answer here lists the maximum heap sizes for various phone models; they range from 16 to 256 MB.

Note that Java stores strings as UTF-16 codes, so each character takes up two bytes. Whether you are appending strings directly or using a StringBuilder (much better), you will occasionally need twice as much memory: one to store the existing string and one to store the new string/buffer when it needs to be expanded.

Considered all together, the maximum string length is likely to be something on the order of 4 to 64 million characters, depending on the phone (far below the theoretical maximum of 231-1).

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
8

Bill the lizard answered in this question

You should be able to get a String of length Integer.MAX_VALUE (always 2147483647 (231 - 1) by the Java specification, the maximum size of an array, which the String class uses for internal storage) or half your maximum heap size (since each character is two bytes), whichever is smaller.

However, for your concatenating sequences of Strings you should be using a StringBuilder which is made for such purposes.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
Sednus
  • 2,095
  • 1
  • 18
  • 35
  • OP is asking for practical limits, not theoretical limits imposed by the Java language. Good point about using `StringBuilder`, though. – Ted Hopp Mar 12 '13 at 18:40