|
Author |
Topic: maximum dimension for a String (Read 402 times) |
|
marcello
|
maximum dimension for a String
« on: Feb 14th, 2005, 5:54pm » |
|
Hi, probably this is a silly question but... how big (in byte) can a String be? Code: String s; //how big can s be? |
| Thanks, Marcello
|
|
|
|
st33d
|
Re: maximum dimension for a String
« Reply #1 on: Feb 15th, 2005, 1:41am » |
|
Haven't got the byte value for you, some stuff on the net says that a string might be 2*length in bytes but personally I've no idea. Perhaps someone else can fill in the blanks. But this memory buster code below and following output should give some kind of indication. Code: String d1 = "a"; void setup(){} void loop(){ d1 = d1+d1; println (d1.length()); } |
| Code: 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 java.lang.OutOfMemoryError |
| That's just my computer though. I've no idea if another would return a different value.
|
I could murder a pint.
|
|
|
marcello
|
Re: maximum dimension for a String
« Reply #2 on: Feb 15th, 2005, 3:17pm » |
|
thank you st33d, i ran your code on my computer and i got the same result. It is a good answer i think Marcello
|
« Last Edit: Feb 15th, 2005, 3:18pm by marcello » |
|
|
|
|
fry
|
Re: maximum dimension for a String
« Reply #3 on: Feb 15th, 2005, 4:00pm » |
|
for what it's worth, in practical terms, you'll probably never need a String that enormous. (and if you do, you probably need to structure things differently in your program). the OutOfMemory may just be hitting the edge of the default memory that's allocated to processing (try modifying the parameters in run.bat) and st33d is correct re: 2*length in bytes is the size of the String. each character of the String is stored as a 16-bit unicode character which provides fancy multi-language support (a "char" in Java is 2 bytes).
|
|
|
|
liminal
|
Re: maximum dimension for a String
« Reply #4 on: Feb 15th, 2005, 6:49pm » |
|
Don't know if this is the actual limit, but Strings internally store their characters in an array. Arrays are indexed with int's. So the upper limit on the array size is Integer.MAX_VALUE = (2^31) = 2147483648. As fry pointed out, each char requires 2 bytes, so the size of such an array would be 2147483648 * 2 = 4294967296 bytes Ignoring proper conversions between bytes and kilo/mega/giga bytes, that translates into about 4GB of memory(!!!) So if you've got 4+ GB of RAM plus swap on your machine, try setting the max JVM heap size to something enormous like 5GB and seeing what happens. (The command line option in the .bat file would be something like -mx5120m ) Good luck!
|
« Last Edit: Feb 16th, 2005, 5:12pm by liminal » |
|
|
|
|
|