How many strings can Java handle?
If I have
String MyArray[400000];
Is there any danger that Java will have a stack overflow or some such?
What kind of overhead is there with the String object?
I heard the stack is 64 MB -- is that the case (as of JRE 1.6)?
64 MB divided by 400,000 is 160 bytes --
Matthew
[316 byte] By [
CathInfoa] at [2007-11-27 4:49:16]

Do you have access to a compiler? How long would it take to write a quick test?
Yes, I have access to a compiler.
But I am looking for intelligent information -- I don't think a simple For loop will tell me what I need to know.
I have other variables, built-in Java classes, the program itself, etc. also using system memory. If I knew (from the documentation) how much heap space there was, it would help me to calculate what I can consume memory-wise.
Matthew
You can control the size of the stack and the heap with command-line arguments when starting the VM.It's the heap, not the stack, that matters here.
jverda at 2007-7-12 10:02:20 >

> program itself, etc. also using system memory. If I
> knew (from the documentation) how much heap space
> there was, it would help me to calculate what I can
> consume memory-wise.
Probably not very accurately, and unless your program is very unusual in that it always deals with a fixed amount of data, the number would be fairly meaningless anyway.
jverda at 2007-7-12 10:02:20 >

http://java.sun.com/javase/6/docs/technotes/tools/windows/java.htmlSee command line options Xmx and Xms. And consider using a database ;-)
How hard would it be to distribute a Jar file that would enable this switch automatically:
"java -Xmx80m"
I suppose I could write a batch file to do it, but right now it's so nice to click on a Jar file and have it launch...
Also, would this work for Windows AND Linux?
The documentation said that it picks the value "based on system configuration". But my system has 1GB RAM, and Java Runtime still seems to pick around 64 MB.
Thanks,
Matthew
CathInfo,
If i wanted to solve your problem this is how I would do it.
A reference takes up 4 bytes in java 2^(4*8) = 4,294,967,296
I would take this to mean that Java can reference 4 billion
unique Objects.
If each Object used 1 byte you would need 4 gigabytes of RAM.
I have 2 gigs of RAM and I dont know of any Objects that take
up 1 byte so this question is pointless.
Woops I believe I misread your question. Im sorry.
I thought you were asking how many Strings Java could hold.
If you were trying to load 400,000 Strings in RAM this is how
you could figure out the memory requirements.
Every String has these fields:
private final char value[];
private final int offset;
private final int count;
private int hash; // Default to 0
Every int is 4 bytes and so is a reference.
Every char is 2 bytes.
So every String is (4) + (4 * 3) + (2 * length) bytes or
20 bytes + (2 * length) bytes minimum.
At 400,000 with an average length of 10 characters that is:
(400 000 + (20 + (2 * 10))) * bytes = 0.381507874 megabytes
+ 4 bytes for each String to holkd a reference to the String =
0.381511688 MBs.
Id say thats your absolute minimum.
Of course, if any of the Strings are substrings of another String their
char[] array is just a reference to the previous one and not a copy of it.