author | mtobiass |
Tue, 04 Mar 2014 09:35:54 +0100 | |
changeset 23053 | 788f0ea9a703 |
parent 23010 | 6dadb192ad81 |
child 23721 | 83da5b498504 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
20784
diff
changeset
|
2 |
* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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. |
|
20784
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
29 |
* In the HotSpot implementation this causes metaspace to be consumed. |
2 | 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 |
||
68 |
static int count = 100000; |
|
69 |
||
23053
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
70 |
Class loadNext() { |
2 | 71 |
|
72 |
// public class TestNNNNNN extends java.lang.Object{ |
|
73 |
// public TestNNNNNN(); |
|
74 |
// Code: |
|
75 |
// 0: aload_0 |
|
76 |
// 1: invokespecial #1; //Method java/lang/Object."<init>":()V |
|
77 |
// 4: return |
|
78 |
// } |
|
79 |
||
80 |
int begin[] = { |
|
81 |
0xca, 0xfe, 0xba, 0xbe, 0x00, 0x00, 0x00, 0x30, |
|
82 |
0x00, 0x0a, 0x0a, 0x00, 0x03, 0x00, 0x07, 0x07, |
|
83 |
0x00, 0x08, 0x07, 0x00, 0x09, 0x01, 0x00, 0x06, |
|
84 |
0x3c, 0x69, 0x6e, 0x69, 0x74, 0x3e, 0x01, 0x00, |
|
85 |
0x03, 0x28, 0x29, 0x56, 0x01, 0x00, 0x04, 0x43, |
|
86 |
0x6f, 0x64, 0x65, 0x0c, 0x00, 0x04, 0x00, 0x05, |
|
87 |
0x01, 0x00, 0x0a, 0x54, 0x65, 0x73, 0x74 }; |
|
88 |
||
89 |
int end [] = { |
|
90 |
0x01, 0x00, 0x10, |
|
91 |
0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, |
|
92 |
0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, |
|
93 |
0x00, 0x21, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, |
|
94 |
0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x04, |
|
95 |
0x00, 0x05, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00, |
|
96 |
0x00, 0x11, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, |
|
97 |
0x00, 0x05, 0x2a, 0xb7, 0x00, 0x01, 0xb1, 0x00, |
|
98 |
0x00, 0x00, 0x00, 0x00, 0x00 }; |
|
99 |
||
100 |
||
101 |
// TestNNNNNN |
|
102 |
||
20784
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
103 |
int load_count = count++; |
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
104 |
if (load_count > 999999) { |
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
105 |
// The test will create a corrupt class file if the count |
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
106 |
// exceeds 999999. Fix the test if this exception is thrown. |
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
107 |
throw new RuntimeException("Load count exceeded"); |
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
108 |
} |
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
109 |
|
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
110 |
String name = "Test" + Integer.toString(load_count); |
2 | 111 |
|
112 |
byte value[]; |
|
113 |
try { |
|
114 |
value = name.substring(4).getBytes("UTF-8"); |
|
115 |
} catch (java.io.UnsupportedEncodingException x) { |
|
116 |
throw new Error(); |
|
117 |
} |
|
118 |
||
119 |
// construct class file |
|
120 |
||
121 |
int len = begin.length + value.length + end.length; |
|
122 |
byte b[] = new byte[len]; |
|
123 |
int i, pos=0; |
|
124 |
for (i=0; i<begin.length; i++) { |
|
125 |
b[pos++] = (byte)begin[i]; |
|
126 |
} |
|
127 |
for (i=0; i<value.length; i++) { |
|
128 |
b[pos++] = value[i]; |
|
129 |
} |
|
130 |
for (i=0; i<end.length; i++) { |
|
131 |
b[pos++] = (byte)end[i]; |
|
132 |
} |
|
133 |
||
134 |
return defineClass(name, b, 0, b.length); |
|
135 |
} |
|
136 |
||
137 |
/* |
|
23053
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
138 |
* Load classes until MemoryPoolMXBean.getUsageThresholdCount() > 0. |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
139 |
* Then wait for the memory threshold notification to be received. |
2 | 140 |
*/ |
141 |
public void run() { |
|
23053
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
142 |
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); |
2 | 143 |
boolean thresholdExceeded = false; |
144 |
||
23053
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
145 |
// Load classes until MemoryPoolMXBean.getUsageThresholdCount() > 0 |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
146 |
while (!thresholdExceeded) { |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
147 |
// the classes are small so we load 10 at a time |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
148 |
for (int i=0; i<10; i++) { |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
149 |
loadNext(); |
2 | 150 |
} |
151 |
||
23053
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
152 |
// check if the threshold has been exceeded |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
153 |
for (MemoryPoolMXBean p : pools) { |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
154 |
if (p.getType() == MemoryType.NON_HEAP && |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
155 |
p.isUsageThresholdSupported() && |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
156 |
p.getUsageThresholdCount() > 0) |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
157 |
{ |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
158 |
thresholdExceeded = true; |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
159 |
break; |
2 | 160 |
} |
161 |
} |
|
162 |
} |
|
23053
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
163 |
|
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
164 |
System.out.println("thresholdExceeded. Waiting for notification"); |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
165 |
while (!listenerInvoked) { |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
166 |
try { |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
167 |
Thread.currentThread().sleep(10); |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
168 |
} catch (InterruptedException x) {} |
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
169 |
} |
2 | 170 |
} |
171 |
} |
|
172 |
||
173 |
public static void main(String args[]) { |
|
23053
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
174 |
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); |
2 | 175 |
|
176 |
// Set threshold of 80% of all NON_HEAP memory pools |
|
177 |
// In the Hotspot implementation this means we should get a notification |
|
20784
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
178 |
// if the CodeCache or metaspace fills up. |
2 | 179 |
|
23053
788f0ea9a703
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents:
23010
diff
changeset
|
180 |
for (MemoryPoolMXBean p : pools) { |
2 | 181 |
if (p.getType() == MemoryType.NON_HEAP && p.isUsageThresholdSupported()) { |
182 |
||
183 |
// set threshold |
|
184 |
MemoryUsage mu = p.getUsage(); |
|
20784
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
185 |
long max = mu.getMax(); |
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
186 |
if (max < 0) { |
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
187 |
throw new RuntimeException("There is no maximum set for " |
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
188 |
+ p.getName() + " memory pool so the test is invalid"); |
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
189 |
} |
c0864e216d36
7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents:
5506
diff
changeset
|
190 |
long threshold = (max * 80) / 100; |
2 | 191 |
|
192 |
p.setUsageThreshold(threshold); |
|
193 |
||
194 |
System.out.println("Selected memory pool for low memory " + |
|
195 |
"detection."); |
|
196 |
MemoryUtil.printMemoryPool(p); |
|
197 |
||
198 |
} |
|
199 |
} |
|
200 |
||
201 |
||
202 |
// Install the listener |
|
203 |
||
204 |
MemoryMXBean mm = ManagementFactory.getMemoryMXBean(); |
|
205 |
SensorListener l2 = new SensorListener(); |
|
206 |
||
207 |
NotificationEmitter emitter = (NotificationEmitter) mm; |
|
208 |
emitter.addNotificationListener(l2, null, null); |
|
209 |
||
210 |
// Start the thread loading classes |
|
211 |
||
212 |
Thread thr = new Thread(new BoundlessLoaderThread()); |
|
213 |
thr.start(); |
|
214 |
||
215 |
// Wait for the thread to terminate |
|
216 |
try { |
|
217 |
thr.join(); |
|
218 |
} catch (InterruptedException x) { |
|
219 |
throw new RuntimeException(x); |
|
220 |
} |
|
221 |
||
222 |
if (listenerInvoked) { |
|
223 |
System.out.println("Notification received - test passed."); |
|
224 |
} else { |
|
225 |
throw new RuntimeException("Test failed - notification not received!"); |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
} |