blob: 788c30ff40d0a2cd63755784fcfc8aa1f17cef13 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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 perm space to be consumed.
30 * Test completes when we the notification is received or an OutOfMemory
31 * is generated.
32 */
33
34import java.lang.management.*;
35import javax.management.*;
36import javax.management.openmbean.CompositeData;
37import java.util.*;
38
39public 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
70 Class loadNext() throws ClassNotFoundException {
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
103 String name = "Test" + Integer.toString(count++);
104
105 byte value[];
106 try {
107 value = name.substring(4).getBytes("UTF-8");
108 } catch (java.io.UnsupportedEncodingException x) {
109 throw new Error();
110 }
111
112 // construct class file
113
114 int len = begin.length + value.length + end.length;
115 byte b[] = new byte[len];
116 int i, pos=0;
117 for (i=0; i<begin.length; i++) {
118 b[pos++] = (byte)begin[i];
119 }
120 for (i=0; i<value.length; i++) {
121 b[pos++] = value[i];
122 }
123 for (i=0; i<end.length; i++) {
124 b[pos++] = (byte)end[i];
125 }
126
127 return defineClass(name, b, 0, b.length);
128 }
129
130 /*
131 * Run method for thread that continuously loads classes.
132 *
133 * Note: Once the usage threshold has been exceeded the low memory
134 * detector thread will attempt to deliver its notification - this can
135 * potentially create a race condition with this thread contining to
136 * fill up perm space. To avoid the low memory detector getting an OutOfMemory
137 * we throttle this thread once the threshold has been exceeded.
138 */
139 public void run() {
140 List pools = ManagementFactory.getMemoryPoolMXBeans();
141 boolean thresholdExceeded = false;
142
143 for (;;) {
144 try {
145 // the classes are small so we load 10 at a time
146 for (int i=0; i<10; i++) {
147 loadNext();
148 }
149 } catch (ClassNotFoundException x) {
150 return;
151 }
152 if (listenerInvoked) {
153 return;
154 }
155
156 // if threshold has been exceeded we put in a delay to allow
157 // the low memory detector do its job.
158 if (thresholdExceeded) {
159 try {
160 Thread.currentThread().sleep(100);
161 } catch (InterruptedException x) { }
162 } else {
163 // check if the threshold has been exceeded
164 ListIterator i = pools.listIterator();
165 while (i.hasNext()) {
166 MemoryPoolMXBean p = (MemoryPoolMXBean) i.next();
167 if (p.getType() == MemoryType.NON_HEAP &&
168 p.isUsageThresholdSupported())
169 {
170 thresholdExceeded = p.isUsageThresholdExceeded();
171 }
172 }
173 }
174 }
175 }
176 }
177
178 public static void main(String args[]) {
179 ListIterator iter = ManagementFactory.getMemoryPoolMXBeans().listIterator();
180
181 // Set threshold of 80% of all NON_HEAP memory pools
182 // In the Hotspot implementation this means we should get a notification
183 // if the CodeCache or perm generation fills up.
184
185 while (iter.hasNext()) {
186 MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
187 if (p.getType() == MemoryType.NON_HEAP && p.isUsageThresholdSupported()) {
188
189 // set threshold
190 MemoryUsage mu = p.getUsage();
191 long threshold = (mu.getMax() * 80) / 100;
192
193 p.setUsageThreshold(threshold);
194
195 System.out.println("Selected memory pool for low memory " +
196 "detection.");
197 MemoryUtil.printMemoryPool(p);
198
199 }
200 }
201
202
203 // Install the listener
204
205 MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
206 SensorListener l2 = new SensorListener();
207
208 NotificationEmitter emitter = (NotificationEmitter) mm;
209 emitter.addNotificationListener(l2, null, null);
210
211 // Start the thread loading classes
212
213 Thread thr = new Thread(new BoundlessLoaderThread());
214 thr.start();
215
216 // Wait for the thread to terminate
217 try {
218 thr.join();
219 } catch (InterruptedException x) {
220 throw new RuntimeException(x);
221 }
222
223 if (listenerInvoked) {
224 System.out.println("Notification received - test passed.");
225 } else {
226 throw new RuntimeException("Test failed - notification not received!");
227 }
228 }
229
230}