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...

4
  • 3
    See this link stackoverflow.com/questions/15460779/… Commented Mar 12, 2014 at 6:42
  • Limit of the Integer number Commented Mar 12, 2014 at 6:43
  • This looks useful. Commented Mar 12, 2014 at 6: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? Commented Mar 12, 2014 at 6:47

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

"You can go up to 4GB for 32 bit JAVA" - While technically correct, Windows will only allow you to allocate 3gb - just saying ;)
It's not correct - you cannot allocate a single array longer than Integer.MAX_VALUE using new
Good thinking if the question is regarding number of elements, if it is regarding the size of the array still correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.