TestSmallHeap.java revision 11833:1cbffa2beba6
1160072Simp/*
2160072Simp * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
3160072Simp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4160072Simp *
5160072Simp * This code is free software; you can redistribute it and/or modify it
6160072Simp * under the terms of the GNU General Public License version 2 only, as
7160072Simp * published by the Free Software Foundation.
8160072Simp *
9160072Simp * This code is distributed in the hope that it will be useful, but WITHOUT
10160072Simp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11160072Simp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12160072Simp * version 2 for more details (a copy is included in the LICENSE file that
13185265Simp * accompanied this code).
14185265Simp *
15185265Simp * You should have received a copy of the GNU General Public License version
16185265Simp * 2 along with this work; if not, write to the Free Software Foundation,
17185265Simp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18185265Simp *
19185265Simp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20185265Simp * or visit www.oracle.com if you need additional information or have any
21185265Simp * questions.
22185265Simp */
23185265Simp
24160072Simp/**
25160072Simp * @test TestSmallHeap
26160072Simp * @bug 8067438 8152239
27160072Simp * @requires vm.gc=="null"
28160072Simp * @summary Verify that starting the VM with a small heap works
29234281Smarius * @library /test/lib
30160072Simp * @modules java.base/jdk.internal.misc
31234281Smarius * @modules java.management/sun.management
32234281Smarius * @build sun.hotspot.WhiteBox
33234281Smarius * @run main ClassFileInstaller sun.hotspot.WhiteBox
34234281Smarius * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI TestSmallHeap
35160072Simp */
36160072Simp
37160363Simp/* Note: It would be nice to verify the minimal supported heap size here,
38234281Smarius * but we align the heap size based on the card table size. And the card table
39248911Sian * size is aligned based on the minimal pages size provided by the os. This
40160072Simp * means that on most platforms, where the minimal page size is 4k, we get a
41160072Simp * minimal heap size of 2m but on Solaris/Sparc we have a page size of 8k and
42249232Shselasky * get a minimal heap size of 4m. And on platforms where the page size is 64k
43234281Smarius * we get a minimal heap size of 32m. We never use large pages for the card table.
44234281Smarius *
45234281Smarius * There is also no check in the VM for verifying that the maximum heap size
46234281Smarius * is larger than the supported minimal heap size.
47181884Simp *
48273651Sian * To work around these behaviors this test uses -Xmx4m but then
49160072Simp * calculates what the expected heap size should be. The calculation is a
50160072Simp * simplified version of the code in the VM. We assume that the card table will
51 * use one page. Each byte in the card table corresponds to 512 bytes on the heap.
52 * So, the expected heap size is page_size * 512.
53 *
54 * There is no formal requirement for the minimal value of the maximum heap size
55 * the VM should support. In most cases the VM could start with -Xmx2m.
56 * But with 2m limit GC could be triggered before VM initialization completed.
57 * Therefore we start the VM with 4M heap.
58 */
59
60import jdk.test.lib.Asserts;
61import jdk.test.lib.process.OutputAnalyzer;
62import jdk.test.lib.process.ProcessTools;
63
64import java.util.LinkedList;
65
66import sun.hotspot.WhiteBox;
67
68public class TestSmallHeap {
69
70    public static void main(String[] args) throws Exception {
71        // Do all work in the VM driving the test, the VM
72        // with the small heap size should do as little as
73        // possible to avoid hitting an OOME.
74        WhiteBox wb = WhiteBox.getWhiteBox();
75        int pageSize = wb.getVMPageSize();
76        int heapBytesPerCard = 512;
77        long expectedMaxHeap = pageSize * heapBytesPerCard;
78
79        verifySmallHeapSize("-XX:+UseParallelGC", expectedMaxHeap);
80        verifySmallHeapSize("-XX:+UseSerialGC", expectedMaxHeap);
81        verifySmallHeapSize("-XX:+UseG1GC", expectedMaxHeap);
82        verifySmallHeapSize("-XX:+UseConcMarkSweepGC", expectedMaxHeap);
83    }
84
85    private static void verifySmallHeapSize(String gc, long expectedMaxHeap) throws Exception {
86        long minMaxHeap = 4 * 1024 * 1024;
87        LinkedList<String> vmOptions = new LinkedList<>();
88        vmOptions.add(gc);
89        vmOptions.add("-Xmx" + minMaxHeap);
90        vmOptions.add("-XX:+PrintFlagsFinal");
91        vmOptions.add(VerifyHeapSize.class.getName());
92
93        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(vmOptions.toArray(new String[0]));
94        OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
95        analyzer.shouldHaveExitValue(0);
96
97        expectedMaxHeap = Math.max(expectedMaxHeap, minMaxHeap);
98        long maxHeapSize = Long.parseLong(analyzer.firstMatch("MaxHeapSize.+=\\s+(\\d+)",1));
99        long actualHeapSize = Long.parseLong(analyzer.firstMatch(VerifyHeapSize.actualMsg + "(\\d+)",1));
100        Asserts.assertEQ(maxHeapSize, expectedMaxHeap);
101        Asserts.assertLessThanOrEqual(actualHeapSize, maxHeapSize);
102    }
103}
104
105class VerifyHeapSize {
106    public static final String actualMsg = "Actual heap size: ";
107
108    public static void main(String args[]) {
109        // Avoid string concatenation
110        System.out.print(actualMsg);
111        System.out.println(Runtime.getRuntime().maxMemory());
112    }
113}
114