You are here : Home / Core Java Tutorials / Core Java tutorial in detail / Exceptions Tutorial in java / OutOfMemoryError
Contents of page >
Read : OutOfMemoryError in java
1) Exception in thread threadName - java.lang.OutOfMemoryError: Requested array size exceeds VM limit in java
OutOfMemoryError: Requested array size exceeds VM limit - indicates that the java application tried to allocate an array larger than the heap size.
2) Example of OutOfMemoryError - Requested array size exceeds VM limit in java >
If heap size is 512 MB, and
java application tries to allocate an array of size 1024 MB.
In this case, OutOfMemoryError - Requested array size exceeds VM - will be thrown because java application tried to allocate an array larger than the heap size.
3) How to avoid/solve OutOfMemoryError - Requested array size exceeds VM limit in java?
You must increase the heap size to avoid OutOfMemoryError - Requested array size exceeds VM limit.
Also, you may check the size of array which you are creating, because generally size of array shouldn’t be not that large, if size of array is more than size of java heap it may be a faulty array.
The solution is simply to increase the Xmx parameter to -Xmx512m
4) Example/program in java to generate OutOfMemoryError: Requested array size exceeds VM limit >
-Xmx5m (Here maximum heap memory set to just 5 megabytes, so that we could easily produce OutOfMemoryError: Requested array size exceeds VM limit)
package outofmemory;
/**
*
* Write a program which could throw
* java.lang.OutOfMemoryError : Requested array size exceeds VM limit
*
*/
public class OutOfMemoryErrorRequestedArraySizeExceedsVMlimit {
public static void main(String[] args) {
Integer[] array = new Integer[10000 * 10000]; //Line 11
}
}
|
OUTPUT of program >
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
at outofmemory.OutOfMemoryErrorRequestedArraySizeExceedsVMlimit.main(OutOfMemoryErrorRequestedArraySizeExceedsVMlimit.java:11)
The solution is simply to increase the Xmx parameter to -Xmx512m as we discussed above.
5) Summary -
So in this tutorial we learned OutOfMemoryError: Requested array size exceeds VM limit - indicates that the java application tried to allocate an array larger than the heap size.
Example of OutOfMemoryError - Requested array size exceeds VM limit > If heap size is 512 MB, and java application tries to allocate an array of size 1024 MB.
Avoid/solve OutOfMemoryError - Requested array size exceeds VM limit - increase the heap size.
Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>