62 import sun.hotspot.WhiteBox;
63
64 public class TestSmallHeap {
65
66 public static void main(String[] args) throws Exception {
67 // Do all work in the VM driving the test, the VM
68 // with the small heap size should do as little as
69 // possible to avoid hitting an OOME.
70 WhiteBox wb = WhiteBox.getWhiteBox();
71 int pageSize = wb.getVMPageSize();
72 int heapBytesPerCard = 512;
73 long expectedMaxHeap = pageSize * heapBytesPerCard;
74
75 verifySmallHeapSize("-XX:+UseParallelGC", expectedMaxHeap);
76 verifySmallHeapSize("-XX:+UseSerialGC", expectedMaxHeap);
77 verifySmallHeapSize("-XX:+UseG1GC", expectedMaxHeap);
78 verifySmallHeapSize("-XX:+UseConcMarkSweepGC", expectedMaxHeap);
79 }
80
81 private static void verifySmallHeapSize(String gc, long expectedMaxHeap) throws Exception {
82 LinkedList<String> vmOptions = new LinkedList<>();
83 vmOptions.add(gc);
84 vmOptions.add("-Xmx2m");
85 vmOptions.add("-XX:+PrintFlagsFinal");
86 vmOptions.add(VerifyHeapSize.class.getName());
87
88 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(vmOptions.toArray(new String[0]));
89 OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
90 analyzer.shouldHaveExitValue(0);
91
92 long maxHeapSize = Long.parseLong(analyzer.firstMatch("MaxHeapSize.+=\\s+(\\d+)",1));
93 long actualHeapSize = Long.parseLong(analyzer.firstMatch(VerifyHeapSize.actualMsg + "(\\d+)",1));
94 Asserts.assertEQ(maxHeapSize, expectedMaxHeap);
95 Asserts.assertLessThanOrEqual(actualHeapSize, maxHeapSize);
96 }
97 }
98
99 class VerifyHeapSize {
100 public static final String actualMsg = "Actual heap size: ";
101
102 public static void main(String args[]) {
103 // Avoid string concatenation
104 System.out.print(actualMsg);
105 System.out.println(Runtime.getRuntime().maxMemory());
106 }
107 }
|
62 import sun.hotspot.WhiteBox;
63
64 public class TestSmallHeap {
65
66 public static void main(String[] args) throws Exception {
67 // Do all work in the VM driving the test, the VM
68 // with the small heap size should do as little as
69 // possible to avoid hitting an OOME.
70 WhiteBox wb = WhiteBox.getWhiteBox();
71 int pageSize = wb.getVMPageSize();
72 int heapBytesPerCard = 512;
73 long expectedMaxHeap = pageSize * heapBytesPerCard;
74
75 verifySmallHeapSize("-XX:+UseParallelGC", expectedMaxHeap);
76 verifySmallHeapSize("-XX:+UseSerialGC", expectedMaxHeap);
77 verifySmallHeapSize("-XX:+UseG1GC", expectedMaxHeap);
78 verifySmallHeapSize("-XX:+UseConcMarkSweepGC", expectedMaxHeap);
79 }
80
81 private static void verifySmallHeapSize(String gc, long expectedMaxHeap) throws Exception {
82 long minMaxHeap = 4 * 1024 * 1024;
83 LinkedList<String> vmOptions = new LinkedList<>();
84 vmOptions.add(gc);
85 vmOptions.add("-Xmx" + minMaxHeap);
86 vmOptions.add("-XX:+PrintFlagsFinal");
87 vmOptions.add(VerifyHeapSize.class.getName());
88
89 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(vmOptions.toArray(new String[0]));
90 OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
91 analyzer.shouldHaveExitValue(0);
92
93 expectedMaxHeap = Math.max(expectedMaxHeap, minMaxHeap);
94 long maxHeapSize = Long.parseLong(analyzer.firstMatch("MaxHeapSize.+=\\s+(\\d+)",1));
95 long actualHeapSize = Long.parseLong(analyzer.firstMatch(VerifyHeapSize.actualMsg + "(\\d+)",1));
96 Asserts.assertEQ(maxHeapSize, expectedMaxHeap);
97 Asserts.assertLessThanOrEqual(actualHeapSize, maxHeapSize);
98 }
99 }
100
101 class VerifyHeapSize {
102 public static final String actualMsg = "Actual heap size: ";
103
104 public static void main(String args[]) {
105 // Avoid string concatenation
106 System.out.print(actualMsg);
107 System.out.println(Runtime.getRuntime().maxMemory());
108 }
109 }
|