freesoft_2000 said:
Hi everyone,
I have a rather silly question but what is the maximum
amount of characters the String object can hold?
"How deep is a hole?"
Since a String's length is an int, it can be no longer
than Integer.MAX_VALUE = 2147483647 characters.
Since each character occupies two bytes, such a String
would require slightly less than 4 gigabytes for the character
data itself, plus a little overhead. You'll never actually
get that high on a 32-bit JVM; a 64-bit JVM might be able to
approach this limit.
Of course, the memory for the String's data comes from
the same "pool" that supplies memory for all the other objects
in your program. If the pool is limited to 256 megabytes, say,
and the other objects occupy 100 megabytes of that amount, then
there's no way your String can be longer than about 81 million
characters.
This is what i mean
String str1 = some buffer that contains about 5 million characters as a
string
If "some buffer" is another String, this won't increase the
memory usage: str1 is just a reference to the existing String
and nothing new is created.
If "some buffer" is a StringBuffer and you're using its
toString() method, the memory usage won't increase very much
because the new String and the existing StringBuffer will share
the same big array of characters. (If you later modify the
StringBuffer, the array will be duplicated at that point -- but
as long as it's possible for the String and the StringBuffer to
share the same characters, the JVM arranges for them to do so.)
If "some buffer" is something else whose conversion to a
String requires copying the existing characters, memory usage
will increase by 10 million bytes plus a smidgen (in addition
to the 10 million plus whatever already occupied by "some
buffer").
... and if any of these memory increases, large or small,
cause the JVM to deplete its memory to the point where it can't
garbage-collect enough to keep on running, you'll get an
OutOfMemoryError exception. Usually you don't want to catch
such exceptions, but if it makes sense for your application to
keep on going after failing to duplicate the big String, you
could do so.
What i am afraid is that if the String object may throw an exception if i
return a huge string object say something down the lines of 5-50 million
characters as a string. Will there be stack overflow?
The exception won't occur as a result of returning the big
String, but as the result of constructing it (or of constructing
something else, if your big greedy String has already monopolized
most of the memory). I can't think of any scenario where such a
thing would produce a stack overflow.
An observation: Strings of this size don't arise "normally."
I rather suspect that you're abusing the String as a sort of
catch-all data structure and that you'd probably be better off
using some other data structure entirely. Even if all the data
is textual, lumping it into a single String is probably a mistake.
You don't concatenate an entire book into one big String; you
instead build another kind of data structure and use many Strings
to represent smaller sub-pieces: paragraphs, sentences, lines,
maybe even individual words.
It is not very useful to store J.R.R. Tolkien's magnum opus
in One String To Rule Them All ...