1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test TestHeapDumpOnOutOfMemoryError
  26  * @summary Test verifies that -XX:HeapDumpOnOutOfMemoryError dump heap when OutOfMemory is thrown in heap
  27  * @library /test/lib
  28  * @run driver TestHeapDumpOnOutOfMemoryError run heap
  29  */
  30 
  31 import jdk.test.lib.Asserts;
  32 import jdk.test.lib.classloader.GeneratingClassLoader;
  33 import jdk.test.lib.hprof.HprofParser;
  34 import jdk.test.lib.process.ProcessTools;
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 
  37 import java.io.File;
  38 
  39 public class TestHeapDumpOnOutOfMemoryError {
  40 
  41     public static String HEAP_OOME = "heap";
  42     public static String METASPACE_OOME = "metaspace";
  43 
  44     public static void main(String[] args) throws Exception {
  45         if (args.length == 1) {
  46              try {
  47                 if (args[0].equals(HEAP_OOME)) {
  48                     Object[] oa = new Object[Integer.MAX_VALUE];
  49                     for(int i = 0; i < oa.length; i++) {
  50                         oa[i] = new Object[Integer.MAX_VALUE];
  51                     }
  52                 } else {
  53                     GeneratingClassLoader loader = new GeneratingClassLoader();
  54                     for (int i = 0; ; i++) {
  55                         loader.loadClass(loader.getClassName(i));
  56                     }
  57                 }
  58                 throw new Error("OOME not triggered");
  59             } catch (OutOfMemoryError err) {
  60                 return;
  61             }
  62         }
  63         test(args[1]);
  64     }
  65 
  66     static void test(String type) throws Exception {
  67         String heapdumpFilename = type + ".hprof";
  68         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+HeapDumpOnOutOfMemoryError",
  69                 "-XX:HeapDumpPath=" + heapdumpFilename, "-XX:MaxMetaspaceSize=64m",
  70                 TestHeapDumpOnOutOfMemoryError.class.getName(), type);
  71 
  72         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  73         output.stdoutShouldNotBeEmpty();
  74         output.shouldContain("Dumping heap to " + type + ".hprof");
  75         File dump = new File(heapdumpFilename);
  76         Asserts.assertTrue(dump.exists() && dump.isFile(),
  77                 "Could not find dump file " + dump.getAbsolutePath());
  78 
  79         HprofParser.parse(new File(heapdumpFilename));
  80         System.out.println("PASSED");
  81     }
  82 
  83 }