OutOfMemoryError系列(7): Requested array size exceeds VM limit
https://blog.csdn.net/renfufei/article/details/78170188
This is the seventh article in this series, a list of related articles:
- OutOfMemoryError系列(1): Java heap space
- OutOfMemoryError系列(2): GC overhead limit exceeded
- OutOfMemoryError系列(3): Permgen space
- OutOfMemoryError系列(4): Metaspace
- OutOfMemoryError系列(5): Unable to create new native thread
- OutOfMemoryError系列(6): Out of swap space?
Java platform limits the maximum length of the array. Specific limitations of each version may be slightly different, but the range in 1 ~ 21亿
between.
If the program throws java.lang.OutOfMemoryError: Requested array size exceeds VM limit
an error, it means you want to create an array of length exceeds the limit.
Cause Analysis
This error is caused by the JVM native code thrown before actually allocate memory for the array, JVM will perform a check: the data structure to be allocated in whether the platform can address (addressable) Of course, this mistake than you. the thought it would be much less common.
Rarely see this error because an int using Java as a subscript (index, index) array. In Java, the maximum value is of type int 2^31 – 1 = 2,147,483,647
. Restrictions on most platforms are approximately equal to this value - for example, on a 64-bit MB Pro, Java 1.7 platform can be assigned length 2,147,483,645
, as well Integer.MAX_VALUE-2
) array.
Add a little length, become Integer.MAX_VALUE-1
, it will be thrown as we know it OutOfMemoryError
:
`Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit`
On some platforms, this maximum limit may also be smaller, for example, 32 Linux, OpenJDK 6 above, the array length of around 1.1 billion (about 2^30
) will be thrown " java.lang.OutOfMemoryError: Requested array size exceeds VM limit
" error. To find a specific limit value, a small test may be performed, see specific examples below.
Examples
The following code is used to demonstrate the java.lang.OutOfMemoryError: Requested array size exceeds VM limit
error:
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) { t.printStackTrace(); } }
Wherein, the iterative loop for four times, each time to initialize an array of int from the length Integer.MAX_VALUE-3
increments, to Integer.MAX_VALUE
up to 64 on the Mac OS X platform Hotspot 7, the code will be executed such results similar to the following:
java.lang.OutOfMemoryError: Java heap space
at eu.plumbr.demo.ArraySize.main(ArraySize.java:8) java.lang.OutOfMemoryError: Java heap space at eu.plumbr.demo.ArraySize.main(ArraySize.java:8) java.lang.OutOfMemoryError: Requested array size exceeds VM limit at eu.plumbr.demo.ArraySize.main(ArraySize.java:8) java.lang.OutOfMemoryError: Requested array size exceeds VM limit at eu.plumbr.demo.ArraySize.main(ArraySize.java:8)
Please note that after two iterations thrown java.lang.OutOfMemoryError: Requested array size exceeds VM limit
before the error, first dished out two times java.lang.OutOfMemoryError: Java heap space
wrong. This is because 2^31-1
a number of int memory occupied by more than the default JVM heap memory of 8GB.
This example also shows the relatively rare cause of this mistake - to get restrictions on the size of the array JVM to allocate almost equal to the length Integer.MAX_INT
of the array in this example is running a 64-bit Mac OS X, when Hotspot 7 platform, only two lengths. I will throw this error: Integer.MAX_INT-1
and Integer.MAX_INT
.
solution
The occurrence java.lang.OutOfMemoryError: Requested array size exceeds VM limit
cause of the error might be:
- The array is too large, the final length of the platform exceeds a limit value, but less than
Integer.MAX_INT
- To test the system limits allocation length is greater than the deliberately
2^31-1
array.
The first case, you need to check the service code, confirm whether you really need a big array. If the length of the array can be reduced, then all is well. If not, the data may need to be split into a plurality of blocks, and then loaded in batches as needed.
If the second case, remember, Java array with int value as an index. Therefore, the array elements can not be more than 2^31-1
one. In fact, the code will error at compile time, the message " error: integer number too large
."
. If you do need to process large data sets, it would have to consider adjusting the solution split into a plurality of small pieces, for example, loaded in batches; or abandon the use of the standard library, but their process data structure, such as the use of sun.misc.Unsafe
the class, by Unsafe tools can be as straightforward language like C allocate memory.
Original link: https://plumbr.eu/outofmemoryerror/requested-array-size-exceeds-vm-limit
Translation Date: September 21, 2017
Translators: anchor: http://blog.csdn.net/renfufei