Thu, 07 Jan 2016 02:36:48 -0800
8138745: Implement ExitOnOutOfMemory and CrashOnOutOfMemory in HotSpot
Reviewed-by: dholmes
Contributed-by: cheleswer.sahu@oracle.com
1.1 --- a/src/share/vm/runtime/globals.hpp Fri Dec 25 14:03:45 2015 +0300 1.2 +++ b/src/share/vm/runtime/globals.hpp Thu Jan 07 02:36:48 2016 -0800 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -1279,6 +1279,13 @@ 1.11 "Decay time (in milliseconds) to re-enable bulk rebiasing of a " \ 1.12 "type after previous bulk rebias") \ 1.13 \ 1.14 + product(bool, ExitOnOutOfMemoryError, false, \ 1.15 + "JVM exits on the first occurrence of an out-of-memory error") \ 1.16 + \ 1.17 + product(bool, CrashOnOutOfMemoryError, false, \ 1.18 + "JVM aborts, producing an error log and core/mini dump, on the " \ 1.19 + "first occurrence of an out-of-memory error") \ 1.20 + \ 1.21 /* tracing */ \ 1.22 \ 1.23 notproduct(bool, TraceRuntimeCalls, false, \
2.1 --- a/src/share/vm/utilities/debug.cpp Fri Dec 25 14:03:45 2015 +0300 2.2 +++ b/src/share/vm/utilities/debug.cpp Thu Jan 07 02:36:48 2016 -0800 2.3 @@ -1,5 +1,5 @@ 2.4 /* 2.5 - * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 2.6 + * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 2.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.8 * 2.9 * This code is free software; you can redistribute it and/or modify it 2.10 @@ -302,6 +302,16 @@ 2.11 VMError err(message); 2.12 err.report_java_out_of_memory(); 2.13 } 2.14 + 2.15 + if (CrashOnOutOfMemoryError) { 2.16 + tty->print_cr("Aborting due to java.lang.OutOfMemoryError: %s", message); 2.17 + fatal(err_msg("OutOfMemory encountered: %s", message)); 2.18 + } 2.19 + 2.20 + if (ExitOnOutOfMemoryError) { 2.21 + tty->print_cr("Terminating due to java.lang.OutOfMemoryError: %s", message); 2.22 + exit(3); 2.23 + } 2.24 } 2.25 } 2.26
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/runtime/ErrorHandling/TestCrashOnOutOfMemoryError.java Thu Jan 07 02:36:48 2016 -0800 3.3 @@ -0,0 +1,106 @@ 3.4 +/* 3.5 + * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test TestCrashOnOutOfMemoryError 3.29 + * @summary Test using -XX:+CrashOnOutOfMemoryError 3.30 + * @library /testlibrary 3.31 + * @build jdk.test.lib.* 3.32 + * @run driver TestCrashOnOutOfMemoryError 3.33 + * @bug 8138745 3.34 + */ 3.35 + 3.36 +import com.oracle.java.testlibrary.OutputAnalyzer; 3.37 +import com.oracle.java.testlibrary.ProcessTools; 3.38 +import java.io.BufferedReader; 3.39 +import java.io.File; 3.40 +import java.io.FileInputStream; 3.41 +import java.io.InputStreamReader; 3.42 +import java.io.IOException; 3.43 + 3.44 +public class TestCrashOnOutOfMemoryError { 3.45 + 3.46 + public static void main(String[] args) throws Exception { 3.47 + if (args.length == 1) { 3.48 + // This should guarantee to throw: 3.49 + // java.lang.OutOfMemoryError: Requested array size exceeds VM limit 3.50 + try { 3.51 + Object[] oa = new Object[Integer.MAX_VALUE]; 3.52 + throw new Error("OOME not triggered"); 3.53 + } catch (OutOfMemoryError err) { 3.54 + throw new Error("OOME didn't abort JVM!"); 3.55 + } 3.56 + } 3.57 + // else this is the main test 3.58 + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+CrashOnOutOfMemoryError", 3.59 + "-Xmx64m", TestCrashOnOutOfMemoryError.class.getName(),"throwOOME"); 3.60 + OutputAnalyzer output = new OutputAnalyzer(pb.start()); 3.61 + int exitValue = output.getExitValue(); 3.62 + if (0 == exitValue) { 3.63 + //expecting a non zero value 3.64 + throw new Error("Expected to get non zero exit value"); 3.65 + } 3.66 + 3.67 + /* Output should look something like this. The actual text will depend on the OS and its core dump processing. 3.68 + Aborting due to java.lang.OutOfMemoryError: Requested array size exceeds VM limit 3.69 + # To suppress the following error report, specify this argument 3.70 + # after -XX: or in .hotspotrc: SuppressErrorAt=/debug.cpp:303 3.71 + # 3.72 + # A fatal error has been detected by the Java Runtime Environment: 3.73 + # 3.74 + # Internal Error (/home/cheleswer/Desktop/jdk9/dev/hotspot/src/share/vm/utilities/debug.cpp:303), pid=6212, tid=6213 3.75 + # fatal error: OutOfMemory encountered: Requested array size exceeds VM limit 3.76 + # 3.77 + # JRE version: OpenJDK Runtime Environment (9.0) (build 1.9.0-internal-debug-cheleswer_2015_10_20_14_32-b00) 3.78 + # Java VM: OpenJDK 64-Bit Server VM (1.9.0-internal-debug-cheleswer_2015_10_20_14_32-b00, mixed mode, tiered, compressed oops, serial gc, linux-amd64) 3.79 + # Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport %p %s %c %P" (or dumping to 3.80 + /home/cheleswer/Desktop/core.6212) 3.81 + # 3.82 + # An error report file with more information is saved as: 3.83 + # /home/cheleswer/Desktop/hs_err_pid6212.log 3.84 + # 3.85 + # If you would like to submit a bug report, please visit: 3.86 + # http://bugreport.java.com/bugreport/crash.jsp 3.87 + # 3.88 + Current thread is 6213 3.89 + Dumping core ... 3.90 + Aborted (core dumped) 3.91 + */ 3.92 + output.shouldContain("Aborting due to java.lang.OutOfMemoryError: Requested array size exceeds VM limit"); 3.93 + // extract hs-err file 3.94 + String hs_err_file = output.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1); 3.95 + if (hs_err_file == null) { 3.96 + throw new Error("Did not find hs-err file in output.\n"); 3.97 + } 3.98 + 3.99 + /* 3.100 + * Check if hs_err files exist or not 3.101 + */ 3.102 + File f = new File(hs_err_file); 3.103 + if (!f.exists()) { 3.104 + throw new Error("hs-err file missing at "+ f.getAbsolutePath() + ".\n"); 3.105 + } 3.106 + 3.107 + System.out.println("PASSED"); 3.108 + } 3.109 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/runtime/ErrorHandling/TestExitOnOutOfMemoryError.java Thu Jan 07 02:36:48 2016 -0800 4.3 @@ -0,0 +1,63 @@ 4.4 +/* 4.5 + * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/* 4.28 + * @test TestExitOnOutOfMemoryError 4.29 + * @summary Test using -XX:ExitOnOutOfMemoryError 4.30 + * @library /testlibrary 4.31 + * @build jdk.test.lib.* 4.32 + * @run driver TestExitOnOutOfMemoryError 4.33 + * @bug 8138745 4.34 + */ 4.35 + 4.36 +import com.oracle.java.testlibrary.ProcessTools; 4.37 +import com.oracle.java.testlibrary.OutputAnalyzer; 4.38 + 4.39 +public class TestExitOnOutOfMemoryError { 4.40 + 4.41 + public static void main(String[] args) throws Exception { 4.42 + if (args.length == 1) { 4.43 + // This should guarantee to throw: 4.44 + // java.lang.OutOfMemoryError: Requested array size exceeds VM limit 4.45 + try { 4.46 + Object[] oa = new Object[Integer.MAX_VALUE]; 4.47 + throw new Error("OOME not triggered"); 4.48 + } catch (OutOfMemoryError err) { 4.49 + throw new Error("OOME didn't terminate JVM!"); 4.50 + } 4.51 + } 4.52 + 4.53 + // else this is the main test 4.54 + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+ExitOnOutOfMemoryError", 4.55 + "-Xmx64m", TestExitOnOutOfMemoryError.class.getName(), "throwOOME"); 4.56 + OutputAnalyzer output = new OutputAnalyzer(pb.start()); 4.57 + 4.58 + /* 4.59 + * Actual output should look like this: 4.60 + * Terminating due to java.lang.OutOfMemoryError: Requested array size exceeds VM limit 4.61 + */ 4.62 + output.shouldHaveExitValue(3); 4.63 + output.shouldContain("Terminating due to java.lang.OutOfMemoryError: Requested array size exceeds VM limit"); 4.64 + System.out.println("PASSED"); 4.65 + } 4.66 +}