Path: blob/master/test/hotspot/jtreg/gc/TestSmallHeap.java
40930 views
/*1* Copyright (c) 2014, 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*/2223package gc;2425/**26* @test TestSmallHeap27* @bug 8067438 815223928* @summary Verify that starting the VM with a small heap works29* @library /test/lib30* @modules java.base/jdk.internal.misc31* @build sun.hotspot.WhiteBox32* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox33* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI gc.TestSmallHeap34*/3536/* Note: It would be nice to verify the minimal supported heap size here,37* but we align the heap size based on the card table size. And the card table38* size is aligned based on the minimal pages size provided by the os. This39* means that on most platforms, where the minimal page size is 4k, we get a40* minimal heap size of 2m but on Solaris/Sparc we have a page size of 8k and41* get a minimal heap size of 4m. And on platforms where the page size is 64k42* we get a minimal heap size of 32m. We never use large pages for the card table.43*44* There is also no check in the VM for verifying that the maximum heap size45* is larger than the supported minimal heap size.46*47* To work around these behaviors this test uses -Xmx4m but then48* calculates what the expected heap size should be. The calculation is a49* simplified version of the code in the VM. We assume that the card table will50* use one page. Each byte in the card table corresponds to 512 bytes on the heap.51* So, the expected heap size is page_size * 512.52*53* There is no formal requirement for the minimal value of the maximum heap size54* the VM should support. In most cases the VM could start with -Xmx2m.55* But with 2m limit GC could be triggered before VM initialization completed.56* Therefore we start the VM with 4M heap.57*/5859import jdk.test.lib.Asserts;60import jdk.test.lib.process.OutputAnalyzer;61import jdk.test.lib.process.ProcessTools;6263import java.util.LinkedList;6465import jtreg.SkippedException;66import sun.hotspot.WhiteBox;67import sun.hotspot.gc.GC;6869public class TestSmallHeap {7071public static void main(String[] args) throws Exception {72// Do all work in the VM driving the test, the VM73// with the small heap size should do as little as74// possible to avoid hitting an OOME.75WhiteBox wb = WhiteBox.getWhiteBox();76int pageSize = wb.getVMPageSize();77int heapBytesPerCard = 512;78long expectedMaxHeap = pageSize * heapBytesPerCard;79boolean noneGCSupported = true;8081if (GC.Parallel.isSupported()) {82noneGCSupported = false;83verifySmallHeapSize("-XX:+UseParallelGC", expectedMaxHeap);84}85if (GC.Serial.isSupported()) {86noneGCSupported = false;87verifySmallHeapSize("-XX:+UseSerialGC", expectedMaxHeap);88}89if (GC.G1.isSupported()) {90noneGCSupported = false;91verifySmallHeapSize("-XX:+UseG1GC", expectedMaxHeap);92}93if (noneGCSupported) {94throw new SkippedException("Skipping test because none of Parallel/Serial/G1 is supported.");95}96}9798private static void verifySmallHeapSize(String gc, long expectedMaxHeap) throws Exception {99long minMaxHeap = 4 * 1024 * 1024;100ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(101gc,102"-Xmx" + minMaxHeap,103"-XX:+PrintFlagsFinal",104VerifyHeapSize.class.getName());105OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());106analyzer.shouldHaveExitValue(0);107108expectedMaxHeap = Math.max(expectedMaxHeap, minMaxHeap);109long maxHeapSize = Long.parseLong(analyzer.firstMatch("MaxHeapSize.+=\\s+(\\d+)",1));110long actualHeapSize = Long.parseLong(analyzer.firstMatch(VerifyHeapSize.actualMsg + "(\\d+)",1));111Asserts.assertEQ(maxHeapSize, expectedMaxHeap);112Asserts.assertLessThanOrEqual(actualHeapSize, maxHeapSize);113}114}115116class VerifyHeapSize {117public static final String actualMsg = "Actual heap size: ";118119public static void main(String args[]) {120// Avoid string concatenation121System.out.print(actualMsg);122System.out.println(Runtime.getRuntime().maxMemory());123}124}125126127