Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/MemoryMXBean/MemoryManagement.java
38821 views
1
/*
2
* Copyright (c) 2003, 2014, 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
* @test
26
* @bug 4530538 6980984
27
* @summary Basic unit test of memory management testing:
28
* 1) setUsageThreshold() and getUsageThreshold()
29
* 2) test low memory detection on the old generation.
30
*
31
* @author Mandy Chung
32
*
33
* @build MemoryManagement MemoryUtil
34
* @run main/othervm/timeout=600 MemoryManagement
35
*/
36
37
import java.lang.management.*;
38
import java.util.*;
39
import javax.management.*;
40
import javax.management.openmbean.CompositeData;
41
42
public class MemoryManagement {
43
private static final MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
44
private static final List pools =
45
Collections.synchronizedList(ManagementFactory.getMemoryPoolMXBeans());
46
private static final List managers =
47
Collections.synchronizedList(ManagementFactory.getMemoryManagerMXBeans());
48
private static volatile MemoryPoolMXBean mpool = null;
49
private static volatile boolean trace = false;
50
private static volatile boolean testFailed = false;
51
private static final int NUM_CHUNKS = 2;
52
private static volatile long chunkSize;
53
private static volatile int listenerInvoked = 0;
54
55
static class SensorListener implements NotificationListener {
56
public void handleNotification(Notification notif, Object handback) {
57
String type = notif.getType();
58
if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
59
type.equals(MemoryNotificationInfo.
60
MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
61
62
MemoryNotificationInfo minfo = MemoryNotificationInfo.
63
from((CompositeData) notif.getUserData());
64
65
MemoryUtil.printMemoryNotificationInfo(minfo, type);
66
listenerInvoked++;
67
}
68
}
69
}
70
71
private static long newThreshold;
72
public static void main(String args[]) throws Exception {
73
if (args.length > 0 && args[0].equals("trace")) {
74
trace = true;
75
}
76
77
if (trace) {
78
MemoryUtil.printMemoryPools(pools);
79
MemoryUtil.printMemoryManagers(managers);
80
}
81
82
// Find the Old generation which supports low memory detection
83
ListIterator iter = pools.listIterator();
84
while (iter.hasNext()) {
85
MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
86
if (p.getType() == MemoryType.HEAP &&
87
p.isUsageThresholdSupported()) {
88
mpool = p;
89
if (trace) {
90
System.out.println("Selected memory pool for low memory " +
91
"detection.");
92
MemoryUtil.printMemoryPool(mpool);
93
}
94
break;
95
}
96
}
97
98
SensorListener listener = new SensorListener();
99
NotificationEmitter emitter = (NotificationEmitter) mm;
100
emitter.addNotificationListener(listener, null, null);
101
102
Thread allocator = new AllocatorThread();
103
104
// Now set threshold
105
MemoryUsage mu = mpool.getUsage();
106
long max = mu.getMax();
107
if (max != -1) {
108
chunkSize = (max - mu.getUsed()) / 20;
109
} else { // 6980984
110
System.gc();
111
chunkSize = Runtime.getRuntime().freeMemory()/20;
112
}
113
newThreshold = mu.getUsed() + (chunkSize * NUM_CHUNKS);
114
115
System.out.println("Setting threshold for " + mpool.getName() +
116
" from " + mpool.getUsageThreshold() + " to " + newThreshold +
117
". Current used = " + mu.getUsed());
118
mpool.setUsageThreshold(newThreshold);
119
120
if (mpool.getUsageThreshold() != newThreshold) {
121
throw new RuntimeException("TEST FAILED: " +
122
"Threshold for Memory pool " + mpool.getName() +
123
"is " + mpool.getUsageThreshold() + " but expected to be" +
124
newThreshold);
125
}
126
127
// Start the AllocatorThread to continously allocate memory
128
System.out.println("Starting an AllocatorThread to allocate memory.");
129
allocator.start();
130
131
try {
132
allocator.join();
133
} catch (InterruptedException e) {
134
e.printStackTrace();
135
System.out.println("Unexpected exception.");
136
testFailed = true;
137
}
138
139
if (listenerInvoked == 0) {
140
throw new RuntimeException("No listener is invoked");
141
}
142
143
if (testFailed)
144
throw new RuntimeException("TEST FAILED.");
145
146
System.out.println("Test passed.");
147
148
}
149
150
static class AllocatorThread extends Thread {
151
private List objectPool = new ArrayList();
152
public void run() {
153
int iterations = 0;
154
int numElements = (int) (chunkSize / 4); // minimal object size
155
while (listenerInvoked == 0) {
156
iterations++;
157
if (trace) {
158
System.out.println(" Iteration " + iterations +
159
": before allocation " +
160
mpool.getUsage().getUsed());
161
}
162
163
Object[] o = new Object[numElements];
164
if (iterations <= NUM_CHUNKS) {
165
// only hold a reference to the first NUM_CHUNKS
166
// allocated objects
167
objectPool.add(o);
168
}
169
170
if (trace) {
171
System.out.println(" " +
172
" after allocation " +
173
mpool.getUsage().getUsed());
174
}
175
try {
176
Thread.sleep(100);
177
} catch (InterruptedException e) {
178
e.printStackTrace();
179
System.out.println("Unexpected exception.");
180
testFailed = true;
181
}
182
}
183
184
System.out.println("AllocatedThread finished memory allocation " +
185
" num_iteration = " + iterations);
186
}
187
}
188
189
}
190
191