0

While running my java code i got an error like this.please help me to over this error...i want to know the maximum size of an STRING ARRAY...

user113118
  • 1
  • 1
  • 1
  • 3
    See this link http://stackoverflow.com/questions/15460779/how-to-increase-the-java-heap-size-in-netbeans – Deepu--Java Mar 12 '14 at 06:42
  • Limit of the Integer number – Kick Mar 12 '14 at 06:43
  • [This](http://stackoverflow.com/a/3039805/1255746) looks useful. – Josh M Mar 12 '14 at 06:44
  • 1
    It's hard to tell what the problem is here - are you actually running out of memory in your real application? If so, have you tried changing the maximum heap size? Or did you only get this error while trying to create deliberately-huge string arrays? – Jon Skeet Mar 12 '14 at 06:47

2 Answers2

2

The maximum length for any array at the moment is Integer.MAX_VALUE which is approximately 2G for both 64 and 32 bit VM.

The reason for this that the new operator accepts int as length parameter (for instance new int[intLength]). There are proposals to allow long in a future release of Java.

If your array/String's length is less than Integer.MAX_VALUE, try to increase your max heap size - for instance: -Xmx4g where 4g means 4GB

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
1

the Array size is limited only with the heap size. to increase the heap allocation for your program use the

-Xmx1500m

as a jvm argument when running you application.

java -Xmx2000m .......

You can go up to 4GB for 32 bit JAVA and more on 64bit.

Chamil
  • 805
  • 5
  • 12
  • 2
    *"You can go up to 4GB for 32 bit JAVA"* - While technically correct, Windows will only allow you to allocate 3gb - just saying ;) – MadProgrammer Mar 12 '14 at 06:49
  • It's not correct - you cannot allocate a single array longer than Integer.MAX_VALUE using `new` – Svetlin Zarev Mar 12 '14 at 06:52
  • Good thinking if the question is regarding number of elements, if it is regarding the size of the array still correct. – Chamil Mar 12 '14 at 06:57