33 * @library /lib/testlibrary/
34 * @build jdk.testlibrary.* LowMemoryTest MemoryUtil RunUtil
35 * @run main/timeout=600 LowMemoryTest
36 */
37
38 import java.lang.management.*;
39 import java.util.*;
40 import java.util.concurrent.Phaser;
41 import javax.management.*;
42 import javax.management.openmbean.CompositeData;
43
44 public class LowMemoryTest {
45 private static final MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
46 private static final List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
47 private static final Phaser phaser = new Phaser(2);
48 private static MemoryPoolMXBean mpool = null;
49 private static boolean trace = false;
50 private static boolean testFailed = false;
51 private static final int NUM_TRIGGERS = 5;
52 private static final int NUM_CHUNKS = 2;
53 private static long chunkSize;
54
55 /**
56 * Run the test multiple times with different GC versions.
57 * First with default command line specified by the framework.
58 * Then with GC versions specified by the test.
59 */
60 public static void main(String a[]) throws Throwable {
61 final String main = "LowMemoryTest$TestMain";
62 RunUtil.runTestKeepGcOpts(main);
63 RunUtil.runTestClearGcOpts(main, "-XX:+UseSerialGC");
64 RunUtil.runTestClearGcOpts(main, "-XX:+UseParallelGC");
65 RunUtil.runTestClearGcOpts(main, "-XX:+UseG1GC");
66 RunUtil.runTestClearGcOpts(main, "-XX:+UseConcMarkSweepGC");
67 }
68
69 private static volatile boolean listenerInvoked = false;
70 static class SensorListener implements NotificationListener {
71 @Override
72 public void handleNotification(Notification notif, Object handback) {
73 String type = notif.getType();
74 if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
75 type.equals(MemoryNotificationInfo.
76 MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
77
78 MemoryNotificationInfo minfo = MemoryNotificationInfo.
79 from((CompositeData) notif.getUserData());
80
81 MemoryUtil.printMemoryNotificationInfo(minfo, type);
82 listenerInvoked = true;
83 }
84 }
85 }
86
138 "detection.");
139 MemoryUtil.printMemoryPool(mpool);
140 }
141 break;
142 }
143 }
144
145 TestListener listener = new TestListener();
146 SensorListener l2 = new SensorListener();
147 NotificationEmitter emitter = (NotificationEmitter) mm;
148 emitter.addNotificationListener(listener, null, null);
149 emitter.addNotificationListener(l2, null, null);
150
151 Thread allocator = new AllocatorThread();
152 Thread sweeper = new SweeperThread();
153
154 // Now set threshold
155 MemoryUsage mu = mpool.getUsage();
156 chunkSize = (mu.getMax() - mu.getUsed()) / 20;
157 newThreshold = mu.getUsed() + (chunkSize * NUM_CHUNKS);
158
159 System.out.println("Setting threshold for " + mpool.getName() +
160 " from " + mpool.getUsageThreshold() + " to " + newThreshold +
161 ". Current used = " + mu.getUsed());
162 mpool.setUsageThreshold(newThreshold);
163
164 if (mpool.getUsageThreshold() != newThreshold) {
165 throw new RuntimeException("TEST FAILED: " +
166 "Threshold for Memory pool " + mpool.getName() +
167 "is " + mpool.getUsageThreshold() + " but expected to be" +
168 newThreshold);
169 }
170
171
172 allocator.start();
173 // Force Allocator start first
174 phaser.arriveAndAwaitAdvance();
175 sweeper.start();
176
177
|
33 * @library /lib/testlibrary/
34 * @build jdk.testlibrary.* LowMemoryTest MemoryUtil RunUtil
35 * @run main/timeout=600 LowMemoryTest
36 */
37
38 import java.lang.management.*;
39 import java.util.*;
40 import java.util.concurrent.Phaser;
41 import javax.management.*;
42 import javax.management.openmbean.CompositeData;
43
44 public class LowMemoryTest {
45 private static final MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
46 private static final List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
47 private static final Phaser phaser = new Phaser(2);
48 private static MemoryPoolMXBean mpool = null;
49 private static boolean trace = false;
50 private static boolean testFailed = false;
51 private static final int NUM_TRIGGERS = 5;
52 private static final int NUM_CHUNKS = 2;
53 private static final int YOUNG_GEN_SIZE = 8 * 1024 * 1024;
54 private static long chunkSize;
55
56 /**
57 * Run the test multiple times with different GC versions.
58 * First with default command line specified by the framework.
59 * Then with GC versions specified by the test.
60 */
61 public static void main(String a[]) throws Throwable {
62 final String main = "LowMemoryTest$TestMain";
63 // Use a low young gen size to ensure that the
64 // allocated objects are put in the old gen.
65 final String nmFlag = "-Xmn" + YOUNG_GEN_SIZE;
66 RunUtil.runTestKeepGcOpts(main, nmFlag);
67 RunUtil.runTestClearGcOpts(main, nmFlag, "-XX:+UseSerialGC");
68 RunUtil.runTestClearGcOpts(main, nmFlag, "-XX:+UseParallelGC");
69 RunUtil.runTestClearGcOpts(main, nmFlag, "-XX:+UseG1GC");
70 RunUtil.runTestClearGcOpts(main, nmFlag, "-XX:+UseConcMarkSweepGC");
71 }
72
73 private static volatile boolean listenerInvoked = false;
74 static class SensorListener implements NotificationListener {
75 @Override
76 public void handleNotification(Notification notif, Object handback) {
77 String type = notif.getType();
78 if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
79 type.equals(MemoryNotificationInfo.
80 MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
81
82 MemoryNotificationInfo minfo = MemoryNotificationInfo.
83 from((CompositeData) notif.getUserData());
84
85 MemoryUtil.printMemoryNotificationInfo(minfo, type);
86 listenerInvoked = true;
87 }
88 }
89 }
90
142 "detection.");
143 MemoryUtil.printMemoryPool(mpool);
144 }
145 break;
146 }
147 }
148
149 TestListener listener = new TestListener();
150 SensorListener l2 = new SensorListener();
151 NotificationEmitter emitter = (NotificationEmitter) mm;
152 emitter.addNotificationListener(listener, null, null);
153 emitter.addNotificationListener(l2, null, null);
154
155 Thread allocator = new AllocatorThread();
156 Thread sweeper = new SweeperThread();
157
158 // Now set threshold
159 MemoryUsage mu = mpool.getUsage();
160 chunkSize = (mu.getMax() - mu.getUsed()) / 20;
161 newThreshold = mu.getUsed() + (chunkSize * NUM_CHUNKS);
162
163 // Sanity check. Make sure the chunkSize is large than the YOUNG_GEN_SIZE
164 // If the chunkSize are lower than the YOUNG_GEN_SIZE, we will get intermittent
165 // failures when objects end up in the young gen instead of the old gen.
166 // Tweak the test if this fails.
167 if (chunkSize < YOUNG_GEN_SIZE) {
168 throw new RuntimeException("TEST FAILED: " +
169 " chunkSize: " + chunkSize + " is less than YOUNG_GEN_SIZE: " + YOUNG_GEN_SIZE +
170 " max: " + mu.getMax() + " used: " + mu.getUsed() + " newThreshold: " + newThreshold);
171 }
172
173 System.out.println("Setting threshold for " + mpool.getName() +
174 " from " + mpool.getUsageThreshold() + " to " + newThreshold +
175 ". Current used = " + mu.getUsed());
176 mpool.setUsageThreshold(newThreshold);
177
178 if (mpool.getUsageThreshold() != newThreshold) {
179 throw new RuntimeException("TEST FAILED: " +
180 "Threshold for Memory pool " + mpool.getName() +
181 "is " + mpool.getUsageThreshold() + " but expected to be" +
182 newThreshold);
183 }
184
185
186 allocator.start();
187 // Force Allocator start first
188 phaser.arriveAndAwaitAdvance();
189 sweeper.start();
190
191
|