OutOfMemoryError in java can occur because of many reasons like Java heap space, GC Overhead limit exceeded, requested array size exceeds VM limit and many more. It is an indication of a memory leak .
Listed below are types of java.lang.OutOfMemoryError Error messages:
- java.lang.OutOfMemoryError:Java heap space -> This exception is thrown when JVM heap size if full and the garbage collector is unable to reclaim objects
- java.lang.OutOfMemoryError:Metaspace ->this exception is thrown when Metaspace area is exhausted.
- java.lang.OutOfMemoryError:Unable to create new native thread ->this exception is thrown when java process size has reached its maximum limit(too many threads are spawned in a process)
- java.lang.OutOfMemoryError:GC overhead limit exceeded->This exception is thrown when your application spends too much time doing garbage collection.
- java.lang.OutOfMemoryError:Permgen space -> This exception is thrown when is thrown when PermGen space memory is full.
- java.lang.OutOfMemoryError:Out of swap space -> This exception is thrown when when the JVM memory is full when there are too many processes running on the machine.
- java.lang.OutOfMemoryError:Requested array size exceeds VM limit -> This exception is thrown when application is trying to allocate an array whose size exceeds than the Java Virtual Machine.
In this tutorial lets understand about OutOfMemoryError which is thrown when there is limited or insufficient space to allocate an object in the Java heap i.e java.lang.OutOfMemoryError: Java heap space
Program to generate OutOfMemoryError: Java heap space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.j2eereference; import java.util.ArrayList; import java.util.List; public class OutOfMemoryDemo { private static List<String> heapList=new ArrayList<String>(); public static void main(String[] args) { while(true) { heapList.add(new String("Shobhna")); } } } |
OutPut:
Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3210)
at java.util.Arrays.copyOf(Arrays.java:3181)
at java.util.ArrayList.grow(ArrayList.java:261)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
at java.util.ArrayList.add(ArrayList.java:458)
at com.j2eereference.OutOfMemoryDemo.main(OutOfMemoryDemo.java:11)
How to solve OutOfMemoryError – Java heap space ?
To solve OutOfMemoryError we need to increase the heap size using jvm parameter –Xmx
How to use -Xmx VM option in java
1) If you want to set the maximum heap size of JVM to 1024 bytes then use below command
java -Xmx1024 OutOfMemoryDemo
2) If you want to set the maximum heap size of JVM to 1024 kilobytes then use below command
java -Xmx1024k OutOfMemoryDemo
3) ) If you want to set the maximum heap size of JVM to 1024 megabytes then use below command
java -Xmx1024m OutOfMemoryDemo
4) If you want to set the maximum heap size of JVM to 1024 gigabyte then use below command
java -Xmx1024g OutOfMemoryDemo