Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/management/MemoryMXBean/ResetPeakMemoryUsage.java
41155 views
1
/*
2
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. 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
* The -XX:MarkSweepAlwaysCompactCount=1 argument below makes sure serial gc
26
* compacts the heap at every full gc so that the usage is correctly updated.
27
*/
28
29
/*
30
* @test
31
* @bug 4892507
32
* @summary Basic Test for MemoryPool.resetPeakUsage()
33
* @author Mandy Chung
34
*
35
* @requires vm.opt.ExplicitGCInvokesConcurrent != "true"
36
* @requires vm.opt.DisableExplicitGC != "true"
37
* @library /test/lib
38
* @modules jdk.management
39
*
40
* @build ResetPeakMemoryUsage MemoryUtil RunUtil
41
* @build sun.hotspot.WhiteBox
42
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
43
* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. ResetPeakMemoryUsage
44
*/
45
46
import java.lang.management.*;
47
import java.lang.ref.WeakReference;
48
import java.util.*;
49
50
import sun.hotspot.code.Compiler;
51
52
public class ResetPeakMemoryUsage {
53
private static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
54
// make public so that it can't be optimized away easily
55
public static Object[] obj;
56
57
/**
58
* Run the test multiple times with different GC versions.
59
* First with default command line specified by the framework.
60
* Then with all GC versions specified by the test.
61
*/
62
public static void main(String a[]) throws Throwable {
63
final String main = "ResetPeakMemoryUsage$TestMain";
64
final String ms = "-Xms256m";
65
final String mn = "-Xmn8m";
66
67
RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseParallelGC");
68
RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseG1GC", "-XX:G1HeapRegionSize=1m");
69
RunUtil.runTestClearGcOpts(main, ms, mn, "-XX:+UseSerialGC",
70
"-XX:MarkSweepAlwaysCompactCount=1");
71
}
72
73
private static class TestMain {
74
public static void main(String[] argv) {
75
List pools = ManagementFactory.getMemoryPoolMXBeans();
76
ListIterator iter = pools.listIterator();
77
boolean found = false;
78
while (iter.hasNext()) {
79
MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
80
// only check heap pools that support usage threshold
81
// this is typically only the old generation space
82
// since the other spaces are expected to get filled up
83
if (p.getType() == MemoryType.HEAP &&
84
p.isUsageThresholdSupported())
85
{
86
found = true;
87
testPool(p);
88
}
89
}
90
if (!found) {
91
throw new RuntimeException("No heap pool found");
92
}
93
}
94
}
95
96
private static void testPool(MemoryPoolMXBean mpool) {
97
System.out.println("Selected memory pool: ");
98
MemoryUtil.printMemoryPool(mpool);
99
100
MemoryUsage usage0 = mpool.getUsage();
101
MemoryUsage peak0 = mpool.getPeakUsage();
102
103
// use a size that is larger than the young generation and G1 regions
104
// to force the array into the old gen
105
int largeArraySize = 9 * 1000 * 1000;
106
107
System.out.println("Before big object array (of size "+largeArraySize+") is allocated: ");
108
printMemoryUsage(usage0, peak0);
109
110
obj = new Object[largeArraySize];
111
WeakReference<Object> weakRef = new WeakReference<>(obj);
112
113
MemoryUsage usage1 = mpool.getUsage();
114
MemoryUsage peak1 = mpool.getPeakUsage();
115
System.out.println("After the object is allocated: ");
116
printMemoryUsage(usage1, peak1);
117
118
if (usage1.getUsed() <= usage0.getUsed()) {
119
throw new RuntimeException(
120
formatSize("Before allocation: used", usage0.getUsed()) +
121
" expected to be < " +
122
formatSize("After allocation: used", usage1.getUsed()));
123
}
124
125
if (peak1.getUsed() <= peak0.getUsed()) {
126
throw new RuntimeException(
127
formatSize("Before allocation: peak", peak0.getUsed()) +
128
" expected to be < " +
129
formatSize("After allocation: peak", peak1.getUsed()));
130
}
131
132
133
// The object is now garbage and do a GC
134
// memory usage should drop
135
obj = null;
136
137
//This will cause sure shot GC unlike Runtime.gc() invoked by mbean.gc()
138
while(weakRef.get() != null) {
139
mbean.gc();
140
}
141
142
MemoryUsage usage2 = mpool.getUsage();
143
MemoryUsage peak2 = mpool.getPeakUsage();
144
System.out.println("After GC: ");
145
printMemoryUsage(usage2, peak2);
146
147
if (usage2.getUsed() >= usage1.getUsed()) {
148
throw new RuntimeException(
149
formatSize("Before GC: used", usage1.getUsed()) + " " +
150
" expected to be > " +
151
formatSize("After GC: used", usage2.getUsed()));
152
}
153
154
mpool.resetPeakUsage();
155
156
MemoryUsage usage3 = mpool.getUsage();
157
MemoryUsage peak3 = mpool.getPeakUsage();
158
System.out.println("After resetPeakUsage: ");
159
printMemoryUsage(usage3, peak3);
160
161
if (peak3.getUsed() != usage3.getUsed()) {
162
throw new RuntimeException(
163
formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
164
" expected to be equal to " +
165
formatSize("current used", usage3.getUsed()));
166
}
167
168
if (peak3.getUsed() >= peak2.getUsed()) {
169
throw new RuntimeException(
170
formatSize("After resetting peak: peak", peak3.getUsed()) + " " +
171
" expected to be < " +
172
formatSize("previous peak", peak2.getUsed()));
173
}
174
175
System.out.println(RunUtil.successMessage);
176
}
177
178
private static String INDENT = " ";
179
private static void printMemoryUsage(MemoryUsage current, MemoryUsage peak) {
180
System.out.println("Current Usage: ");
181
MemoryUtil.printMemoryUsage(current);
182
System.out.println("Peak Usage: ");
183
MemoryUtil.printMemoryUsage(peak);
184
185
}
186
private static String formatSize(String name, long value) {
187
StringBuffer buf = new StringBuffer(name + " = " + value);
188
if (value > 0) {
189
buf.append(" (" + (value >> 10) + "K)");
190
}
191
return buf.toString();
192
}
193
}
194
195