Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/management/MemoryMXBean/LowMemoryTest2.java
41155 views
1
/*
2
* Copyright (c) 2004, 2013, 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 low memory detection of non-heap memory pool.
26
*
27
* The test set a listener to be notified when any of the non-heap pools
28
* exceed 80%. It then starts a thread that continuously loads classes.
29
* In the HotSpot implementation this causes metaspace to be consumed.
30
* Test completes when we the notification is received or an OutOfMemory
31
* is generated.
32
*/
33
34
import java.lang.management.*;
35
import javax.management.*;
36
import javax.management.openmbean.CompositeData;
37
import java.util.*;
38
39
public class LowMemoryTest2 {
40
41
private static volatile boolean listenerInvoked = false;
42
43
private static String INDENT = " ";
44
45
static class SensorListener implements NotificationListener {
46
public void handleNotification(Notification notif, Object handback) {
47
String type = notif.getType();
48
if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
49
type.equals(MemoryNotificationInfo.
50
MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
51
listenerInvoked = true;
52
MemoryNotificationInfo minfo = MemoryNotificationInfo.
53
from((CompositeData) notif.getUserData());
54
55
System.out.print("Notification for " + minfo.getPoolName());
56
System.out.print(" [type = " + type);
57
System.out.println(" count = " + minfo.getCount() + "]");
58
System.out.println(INDENT + "usage = " + minfo.getUsage());
59
}
60
}
61
}
62
63
// Loads classes Test100001, Test100002, .... until OutOfMemoryErrror or
64
// low memory notification
65
66
static class BoundlessLoaderThread extends ClassLoader implements Runnable {
67
private final List<MemoryPoolMXBean> pools;
68
69
public BoundlessLoaderThread(List<MemoryPoolMXBean> pools) {
70
this.pools = pools;
71
}
72
73
static int count = 100000;
74
75
Class loadNext() {
76
77
// public class TestNNNNNN extends java.lang.Object{
78
// public TestNNNNNN();
79
// Code:
80
// 0: aload_0
81
// 1: invokespecial #1; //Method java/lang/Object."<init>":()V
82
// 4: return
83
// }
84
85
int begin[] = {
86
0xca, 0xfe, 0xba, 0xbe, 0x00, 0x00, 0x00, 0x30,
87
0x00, 0x0a, 0x0a, 0x00, 0x03, 0x00, 0x07, 0x07,
88
0x00, 0x08, 0x07, 0x00, 0x09, 0x01, 0x00, 0x06,
89
0x3c, 0x69, 0x6e, 0x69, 0x74, 0x3e, 0x01, 0x00,
90
0x03, 0x28, 0x29, 0x56, 0x01, 0x00, 0x04, 0x43,
91
0x6f, 0x64, 0x65, 0x0c, 0x00, 0x04, 0x00, 0x05,
92
0x01, 0x00, 0x0a, 0x54, 0x65, 0x73, 0x74 };
93
94
int end [] = {
95
0x01, 0x00, 0x10,
96
0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e,
97
0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74,
98
0x00, 0x21, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00,
99
0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x04,
100
0x00, 0x05, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00,
101
0x00, 0x11, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
102
0x00, 0x05, 0x2a, 0xb7, 0x00, 0x01, 0xb1, 0x00,
103
0x00, 0x00, 0x00, 0x00, 0x00 };
104
105
106
// TestNNNNNN
107
108
int load_count = count++;
109
if (load_count > 999999) {
110
// The test will create a corrupt class file if the count
111
// exceeds 999999. Fix the test if this exception is thrown.
112
throw new RuntimeException("Load count exceeded");
113
}
114
115
String name = "Test" + Integer.toString(load_count);
116
117
byte value[];
118
try {
119
value = name.substring(4).getBytes("UTF-8");
120
} catch (java.io.UnsupportedEncodingException x) {
121
throw new Error();
122
}
123
124
// construct class file
125
126
int len = begin.length + value.length + end.length;
127
byte b[] = new byte[len];
128
int i, pos=0;
129
for (i=0; i<begin.length; i++) {
130
b[pos++] = (byte)begin[i];
131
}
132
for (i=0; i<value.length; i++) {
133
b[pos++] = value[i];
134
}
135
for (i=0; i<end.length; i++) {
136
b[pos++] = (byte)end[i];
137
}
138
139
return defineClass(name, b, 0, b.length);
140
}
141
142
/*
143
* Load classes until MemoryPoolMXBean.getUsageThresholdCount() > 0.
144
* Then wait for the memory threshold notification to be received.
145
*/
146
public void run() {
147
// Load classes until MemoryPoolMXBean.getUsageThresholdCount() > 0
148
boolean isThresholdCountSet = false;
149
try {
150
while (!isThresholdCountSet) {
151
// the classes are small so we load 10 at a time
152
for (int i=0; i<10; i++) {
153
loadNext();
154
}
155
156
if (isAnyUsageAboveThreshold(pools)) {
157
// UsageThresholdCount is only updated during GC.
158
// Force GC to update counters.
159
// If we don't force a GC we may get an
160
// OutOfMemoryException before the counters are updated.
161
System.out.println("Force GC");
162
System.gc();
163
}
164
isThresholdCountSet = isAnyThresholdCountSet(pools);
165
}
166
} catch (OutOfMemoryError e) {
167
e.printStackTrace();
168
MemoryUtil.printMemoryPools(pools);
169
throw e;
170
}
171
172
System.out.println("thresholdExceeded. Waiting for notification");
173
while (!listenerInvoked) {
174
try {
175
Thread.currentThread().sleep(10);
176
} catch (InterruptedException x) {}
177
}
178
}
179
180
private boolean isAnyUsageAboveThreshold(List<MemoryPoolMXBean> pools) {
181
for (MemoryPoolMXBean p : pools) {
182
if (p.isUsageThresholdExceeded()) {
183
System.out.println("isAnyUsageAboveThreshold is true for " + p.getName());
184
MemoryUtil.printMemoryPool(p);
185
return true;
186
}
187
}
188
return false;
189
}
190
191
private boolean isAnyThresholdCountSet(List<MemoryPoolMXBean> pools) {
192
for (MemoryPoolMXBean p : pools) {
193
if (p.getUsageThresholdCount() > 0) {
194
System.out.println("isAnyThresholdCountSet is true for " + p.getName());
195
MemoryUtil.printMemoryPool(p);
196
return true;
197
}
198
}
199
return false;
200
}
201
}
202
203
public static void main(String args[]) {
204
// The pools list will only contain the pools that we are interested in.
205
List<MemoryPoolMXBean> pools = new ArrayList<MemoryPoolMXBean>();
206
207
// Set threshold of 80% of all NON_HEAP memory pools
208
// In the Hotspot implementation this means we should get a notification
209
// if the CodeCache or metaspace fills up.
210
211
for (MemoryPoolMXBean p : ManagementFactory.getMemoryPoolMXBeans()) {
212
if (p.getType() == MemoryType.NON_HEAP && p.isUsageThresholdSupported()) {
213
214
// set threshold
215
MemoryUsage mu = p.getUsage();
216
long max = mu.getMax();
217
if (max < 0) {
218
throw new RuntimeException("There is no maximum set for "
219
+ p.getName() + " memory pool so the test is invalid");
220
}
221
long threshold = (max * 80) / 100;
222
223
p.setUsageThreshold(threshold);
224
pools.add(p);
225
226
System.out.println("Selected memory pool for low memory " +
227
"detection.");
228
MemoryUtil.printMemoryPool(p);
229
230
}
231
}
232
233
234
// Install the listener
235
236
MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
237
SensorListener l2 = new SensorListener();
238
239
NotificationEmitter emitter = (NotificationEmitter) mm;
240
emitter.addNotificationListener(l2, null, null);
241
242
// Start the thread loading classes
243
244
Thread thr = new Thread(new BoundlessLoaderThread(pools));
245
thr.start();
246
247
// Wait for the thread to terminate
248
try {
249
thr.join();
250
} catch (InterruptedException x) {
251
throw new RuntimeException(x);
252
}
253
254
if (listenerInvoked) {
255
System.out.println("Notification received - test passed.");
256
} else {
257
throw new RuntimeException("Test failed - notification not received!");
258
}
259
}
260
261
}
262
263