Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/gc/epsilon/TestMemoryMXBeans.java
66644 views
1
/*
2
* Copyright (c) 2017, 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
package gc.epsilon;
26
27
/**
28
* @test TestMemoryMXBeans
29
* @requires vm.gc.Epsilon
30
* @summary Test JMX memory beans
31
* @modules java.base/jdk.internal.misc
32
* java.management
33
*
34
* @run main/othervm -Xmx256m
35
* -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC
36
* gc.epsilon.TestMemoryMXBeans
37
* -1 256
38
*
39
* @run main/othervm -Xmx256m -Xmx256m
40
* -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC
41
* gc.epsilon.TestMemoryMXBeans
42
* 256 256
43
*
44
* @run main/othervm -Xms64m -Xmx256m
45
* -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC
46
* gc.epsilon.TestMemoryMXBeans
47
* 64 256
48
*/
49
50
import java.lang.management.*;
51
52
public class TestMemoryMXBeans {
53
54
static volatile Object sink;
55
56
public static void main(String[] args) throws Exception {
57
if (args.length < 2) {
58
throw new IllegalStateException("Should provide expected heap sizes");
59
}
60
61
long initSize = 1L * Integer.parseInt(args[0]) * 1024 * 1024;
62
long maxSize = 1L * Integer.parseInt(args[1]) * 1024 * 1024;
63
64
testMemoryBean(initSize, maxSize);
65
testAllocs();
66
}
67
68
public static void testMemoryBean(long initSize, long maxSize) {
69
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
70
long heapInit = memoryMXBean.getHeapMemoryUsage().getInit();
71
long heapMax = memoryMXBean.getHeapMemoryUsage().getMax();
72
memoryMXBean.getNonHeapMemoryUsage().getInit(); // value not used
73
memoryMXBean.getNonHeapMemoryUsage().getMax(); // value not used
74
75
if (initSize > 0 && heapInit != initSize) {
76
throw new IllegalStateException("Init heap size is wrong: " + heapInit + " vs " + initSize);
77
}
78
if (maxSize > 0 && heapMax != maxSize) {
79
throw new IllegalStateException("Max heap size is wrong: " + heapMax + " vs " + maxSize);
80
}
81
}
82
83
public static void testAllocs() throws Exception {
84
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
85
86
// Try multiple times, to capture either APIs we call allocate lazily, or the background threads allocating
87
int maxTries = 10;
88
int tries = 0;
89
90
while (true) {
91
// Compute how much we waste during the calls themselves:
92
long heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed();
93
long heapUsed2 = memoryMXBean.getHeapMemoryUsage().getUsed();
94
long adj = heapUsed2 - heapUsed1;
95
96
heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed();
97
sink = new int[1024*1024];
98
heapUsed2 = memoryMXBean.getHeapMemoryUsage().getUsed();
99
100
long diff = heapUsed2 - heapUsed1 - adj;
101
long min = 8 + 4*1024*1024;
102
long max = 16 + 4*1024*1024;
103
if ((min <= diff && diff <= max)) {
104
// Success
105
return;
106
}
107
108
if (tries++ > maxTries) {
109
throw new IllegalStateException("Allocation did not change used space right: " + diff + " should be in [" + min + ", " + max + "]");
110
}
111
112
// Wait and try again
113
Thread.sleep(1000);
114
}
115
}
116
117
}
118
119