Example 1

 

 

import java.util.ArrayList;
import java.util.List;

class SimulateOutOfMemory {

    public static void main(String[] args) {

        List<byte[]> list = new ArrayList<>();
        int index = 1;

        while (true) {
            // 1MB each loop, 1 x 1024 x 1024 = 1048576
            byte[] b = new byte[1048576];
            list.add(b);
            Runtime rt = Runtime.getRuntime();
            System.out.printf("[%d] free memory: %s%n", index++, rt.freeMemory());
        }

    }
}

Example 2

 

import java.util.ArrayList;

class SimulateOutOfMemory {

    public static void main(String[] args) {
        new ArrayList(Integer.MAX_VALUE);
    }
}

 

 

[출처]https://codechacha.com/ko/java-simulate-out-of-memery-error/

 

+ Recent posts