Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8267118

OutOfMemoryError cannot be caught as a Throwable

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 17
    • 17
    • hotspot
    • b25

        import java.util.*;
        public class OOM {
            private static HashMap<Object, Object> store = new HashMap<>();
            public static void main(String... args) {
                int size = Integer.parseInt(args[0]);
                System.out.println("Start");
                try {
                    while (true) {
                        Object o = new Byte[size * 1024];
                        store.put(o, o);
                    }
                } catch (Throwable t) { /// <<<<==Change to "catch (VirtualMachineError t)" and we'll be OK.
                    store = null;
                }
            }
        }

        $ java -Xmx64m OOM.java 100; echo $?
        Start
        Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        1

        $ java -Xmx64m OOM.java 300; echo $?
        Start
        0

        With SerialGC, the OOM can be caught even with size==1

        $ java -Xmx64m -XX:+UseSerialGC OOM.java 1; echo $?
        Start
        0

              dholmes David Holmes
              iklam Ioi Lam
              Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

                Created:
                Updated:
                Resolved: