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

OutOfMemoryError cannot be caught as a Throwable

    XMLWordPrintable

Details

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

    Backports

      Description

        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

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                  Created:
                  Updated:
                  Resolved: