Path: blob/master/test/hotspot/jtreg/runtime/ErrorHandling/TestHeapDumpOnOutOfMemoryError.java
40942 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test TestHeapDumpOnOutOfMemoryError25* @summary Test verifies that -XX:HeapDumpOnOutOfMemoryError dumps heap when OutOfMemory is thrown in heap26* @library /test/lib27* @run driver TestHeapDumpOnOutOfMemoryError run heap28*/2930/*31* @test TestHeapDumpOnOutOfMemoryError32* @summary Test verifies that -XX:HeapDumpOnOutOfMemoryError dumps heap when OutOfMemory is thrown in metaspace.33* @library /test/lib34* @run driver/timeout=240 TestHeapDumpOnOutOfMemoryError run metaspace35*/3637import jdk.test.lib.Asserts;38import jdk.test.lib.Platform;39import jdk.test.lib.classloader.GeneratingClassLoader;40import jdk.test.lib.hprof.HprofParser;41import jdk.test.lib.process.ProcessTools;42import jdk.test.lib.process.OutputAnalyzer;4344import java.io.File;4546public class TestHeapDumpOnOutOfMemoryError {4748public static final String HEAP_OOME = "heap";49public static final String METASPACE_OOME = "metaspace";5051public static void main(String[] args) throws Exception {52if (args.length == 1) {53try {54if (args[0].equals(HEAP_OOME)) {55Object[] oa = new Object[Integer.MAX_VALUE];56for(int i = 0; i < oa.length; i++) {57oa[i] = new Object[Integer.MAX_VALUE];58}59} else {60GeneratingClassLoader loader = new GeneratingClassLoader();61for (int i = 0; ; i++) {62loader.loadClass(loader.getClassName(i));63}64}65throw new Error("OOME not triggered");66} catch (OutOfMemoryError err) {67return;68}69}70test(args[1]);71}7273static void test(String type) throws Exception {74String heapdumpFilename = type + ".hprof";75ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+HeapDumpOnOutOfMemoryError",76"-XX:HeapDumpPath=" + heapdumpFilename,77// Note: When trying to provoke a metaspace OOM we may generate a lot of classes. In debug VMs this78// can cause considerable wait times since:79// - Compiler Dependencies verification iterates the class tree80// - Before exit, the CLDG is checked.81// Both verifications show quadratic time or worse wrt to number of loaded classes. Therefore it82// makes sense to switch one or both off and limit the metaspace size to something sensible.83// Example numbers on a slow ppc64 machine:84// MaxMetaspaceSize=64M - ~60-70K classes - ~20min runtime with all verifications85// MaxMetaspaceSize=16M - ~12-15K classes - ~12sec runtime with all verifications86// MaxMetaspaceSize=16M - ~12-15K classes - VerifyDependencies off - ~3seconds on ppc87"-XX:MaxMetaspaceSize=16m",88"-Xmx128m",89Platform.isDebugBuild() ? "-XX:-VerifyDependencies" : "-Dx",90TestHeapDumpOnOutOfMemoryError.class.getName(), type);9192OutputAnalyzer output = new OutputAnalyzer(pb.start());93output.stdoutShouldNotBeEmpty();94output.shouldContain("Dumping heap to " + type + ".hprof");95File dump = new File(heapdumpFilename);96Asserts.assertTrue(dump.exists() && dump.isFile(),97"Could not find dump file " + dump.getAbsolutePath());9899HprofParser.parse(new File(heapdumpFilename));100System.out.println("PASSED");101}102103}104105106