Generate OutOfMemoryError java heap space 2016-07-06 06:09

OutOfMemoryError means the memory is not enough for your program. After this error Java virtual machine will tell you the detail. When you want solve this problem, you should first of all get to understand it. In this page I will show you how to generate OutOfMemoryError: Java heap space programatically.


public class HeapOOM {
    static class OOMObject {


    public static void main(String[] args) {
        List<OOMObject> list = new ArrayList<>();
        while (true) {
            list.add(new OOMObject());

Before run above simple code we need add arguments like following. These argument will help you generate OutOfMemoryError quick.

-Xms20m -Xmx20m

The output like following

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(
    at java.util.Arrays.copyOf(
    at java.util.ArrayList.grow(
    at java.util.ArrayList.ensureExplicitCapacity(
    at java.util.ArrayList.ensureCapacityInternal(
    at java.util.ArrayList.add(
    at com.henryxi.jvm.HeapOOM.main(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at com.intellij.rt.execution.application.AppMain.main(


OutOfMemoryError: Java heap space means there is no more memory in heap to create new instance. In order to generate this error quickly we add argument -Xms20m -Xmx20m. The first argument means the min memory to run this program is 20m. The second argument means the max memory to run this program is 20m. The result of using both of them is run this program with 20m without increasing. If you meet this error in your program there are two ways to solve it.

  • check your code to avoid instantiating so many object.
  • increase the value of -Xms -Xmx