1 /* 2 * Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 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 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 /** 25 * @test TestHeapAlloc 26 * @key gc 27 * @summary Fail to expand Java heap should not result fatal errors 28 * @requires (vm.debug == true) 29 * @library /test/lib 30 * @modules java.base/jdk.internal.misc 31 * java.management 32 * @ignore // heap is not resized in a conventional manner, ignore for now 33 */ 34 35 import jdk.test.lib.process.ProcessTools; 36 import jdk.test.lib.process.OutputAnalyzer; 37 import jdk.test.lib.Platform; 38 39 import java.util.*; 40 import java.util.concurrent.*; 41 42 public class TestHeapAlloc { 43 44 public static void main(String[] args) throws Exception { 45 if (!Platform.isDebugBuild()) { 46 System.out.println("Test requires debug build. Please silently for release build"); 47 return; 48 } 49 50 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 51 true, 52 "-Xmx1G", 53 "-Xms256M", 54 "-XX:+UnlockExperimentalVMOptions", 55 "-XX:+UseShenandoahGC", 56 "-XX:ShenandoahFailHeapExpansionAfter=50", 57 "-Xlog:gc+region=debug", 58 AllocALotObjects.class.getName()); 59 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 60 output.shouldContain("Artificially fails heap expansion"); 61 output.shouldHaveExitValue(0); 62 } 63 64 public static class AllocALotObjects { 65 public static void main(String[] args) { 66 try { 67 ArrayList<String> list = new ArrayList<>(); 68 long count = 500 * 1024 * 1024 / 16; // 500MB allocation 69 for (long index = 0; index < count; index ++) { 70 String sink = "string " + index; 71 list.add(sink); 72 } 73 74 int index = ThreadLocalRandom.current().nextInt(0, (int)count); 75 System.out.print(list.get(index)); 76 } catch (OutOfMemoryError e) { 77 } 78 } 79 } 80 81 }