• Register
Log In
0 votes

Problem :

I am getting bellow memory error
java.lang.outofmemoryerror requested array size exceeds vm limit
6.9k points

2 Answers

0 votes

Solution :

I doubt you may be using sorts on a large index. 

That is one thing I know can require a large array size with Lucene. 

You might want to try using a 64-bit JVM with these options:

-Xmx6G -XX:MaxPermSize=128M -XX:+UseCompressedOops

The last option will reduce 64-bit memory pointers to 32-bit This typically reduces the memory overhead by about 40%, so it can help stretch your memory significantly.

This will solve your memory related issue.

36.1k points
0 votes

Java has got a limit on the maximum array size your program can allocate. The exact limit is platform-specific but is generally between the somewhere 1 and 2.1 billion elements.

What is the reason?

This error is thrown by the native code within the JVM. This happens before allocating the memory for an array when the JVM performs platform-specific tasks, whether the allocated data structure is addressable in this platform.

  • Your array grows too big and ends up having the size between the platform limit and the integer Max_LIMIT.
  • You try to allocate the arrays larger than 2^31-1 elements to experiment with this limit.


When trying to recreate the java.lang.OutOfMemoryError: Requested array size exceeds VM limit error. Let's look at the following code;

for (int i = 3; i >= 0; i--) {

            try {

                        int[] arr = new int[Integer.MAX_VALUE-i];

                        System.out.format("Successfully initialized an array with %,d elements.\n", Integer.MAX_VALUE-i);

            } catch (Throwable t) {




The example iterates four times and initializes an array of long primitives on each turn. In this program, the size of an array is trying to initialize grows by one with every iteration and finally reaches Integer Max value.


Initialize the array size with integer MAX_VALUE-2 to get rid of such errors;

int [] arr = new int[Integer.MAX_VALUE-2];


3.9k points

Related questions

0 votes
1 answer 88 views
Problem : I am getting bellow error while I execute my JUnit tests exception in thread "main" java.lang.outofmemoryerror: gc overhead limit exceeded
asked Oct 19, 2019 peterlaw 6.9k points
0 votes
1 answer 143 views
Problem : I am getting bellow minecraft error minecraft error: java.lang.outofmemoryerror: gc overhead limit exceeded
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 868 views
Problem : I am getting below error while running my Jersey API in Tomcat 8.5.11 java.lang.illegalstateexception: injectionmanagerfactory not found.
asked Nov 1, 2019 peterlaw 6.9k points
0 votes
1 answer 89 views
Problem : I am new to Servlet programing, while running my Servlet code i am getting bellow error error 500 javax servlet servletexception java lang illegalstateexception
asked Oct 23, 2019 peterlaw 6.9k points
1 vote
1 answer 16 views
Problem : I am very new to Java Programming also I have very recently started using the Intellij as the IDE. I have observed that If I try to use it then sometimes it is delayed a lot. I have tried changing my xms and xmx for the larger heap size (i.e. xms ... the maximum heap size". Because of above error message I have already rolled back my changes. Please let me know how can I fix above error?
asked May 9 stewart 4k points