1 /* 2 * Copyright (c) 2018, Red Hat, Inc. 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 /** 26 * @test id=large 27 * @summary Test allocation of large objects results in OOM, but will not crash the JVM 28 * @requires vm.gc.Shenandoah 29 * @library /test/lib 30 * @run driver TestAllocOutOfMemory large 31 */ 32 33 /** 34 * @test id=heap 35 * @summary Test allocation of a heap-sized object results in OOM, but will not crash the JVM 36 * @requires vm.gc.Shenandoah 37 * @library /test/lib 38 * @run driver TestAllocOutOfMemory heap 39 */ 40 41 /** 42 * @test id=small 43 * @summary Test allocation of small objects results in OOM, but will not crash the JVM 44 * @requires vm.gc.Shenandoah 45 * @library /test/lib 46 * @run driver TestAllocOutOfMemory small 47 */ 48 import jdk.test.lib.process.OutputAnalyzer; 49 import jdk.test.lib.process.ProcessTools; 50 51 public class TestAllocOutOfMemory { 52 53 static volatile Object sink; 54 55 public static void work(int size, int count) throws Exception { 56 Object[] root = new Object[count]; 57 sink = root; 58 for (int c = 0; c < count; c++) { 59 root[c] = new Object[size]; 60 } 61 } 62 63 private static void allocate(String size) throws Exception { 64 switch (size) { 65 case "large": 66 work(1024 * 1024, 16); 67 break; 68 case "heap": 69 work(16 * 1024 * 1024, 1); 70 break; 71 case "small": 72 work(1, 16 * 1024 * 1024); 73 break; 74 default: 75 throw new IllegalArgumentException("Usage: test [large|small|heap]"); 76 } 77 } 78 79 public static void main(String[] args) throws Exception { 80 if (args.length > 1) { 81 // Called from test, size is second argument 82 String size = args[1]; 83 allocate(size); 84 return; 85 } 86 87 // Called from jtreg, size is first argument 88 String size = args[0]; 89 { 90 expectFailure("-Xmx16m", 91 "-XX:+UnlockExperimentalVMOptions", 92 "-XX:+UseShenandoahGC", 93 TestAllocOutOfMemory.class.getName(), 94 "test", size); 95 96 expectFailure("-Xmx16m", 97 "-XX:+UnlockExperimentalVMOptions", 98 "-XX:+UseShenandoahGC", "-XX:ShenandoahGCMode=generational", 99 TestAllocOutOfMemory.class.getName(), 100 "test", size); 101 } 102 103 { 104 expectSuccess("-Xmx1g", 105 "-XX:+UnlockExperimentalVMOptions", 106 "-XX:+UseShenandoahGC", 107 TestAllocOutOfMemory.class.getName(), 108 "test", size); 109 110 expectSuccess("-Xmx1g", 111 "-XX:+UnlockExperimentalVMOptions", 112 "-XX:+UseShenandoahGC", "-XX:ShenandoahGCMode=generational", 113 TestAllocOutOfMemory.class.getName(), 114 "test", size); 115 } 116 } 117 118 private static void expectSuccess(String... args) throws Exception { 119 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args); 120 OutputAnalyzer analyzer = new OutputAnalyzer(pb.start()); 121 analyzer.shouldHaveExitValue(0); 122 analyzer.shouldNotContain("java.lang.OutOfMemoryError: Java heap space"); 123 } 124 125 private static void expectFailure(String... args) throws Exception { 126 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args); 127 OutputAnalyzer analyzer = new OutputAnalyzer(pb.start()); 128 analyzer.shouldHaveExitValue(1); 129 analyzer.shouldContain("java.lang.OutOfMemoryError: Java heap space"); 130 } 131 }