--- a/jdk/test/java/lang/management/MemoryMXBean/CollectionUsageThreshold.java Fri Jun 18 16:16:51 2010 +0100
+++ b/jdk/test/java/lang/management/MemoryMXBean/CollectionUsageThreshold.java Fri Jun 18 09:35:22 2010 -0700
@@ -41,9 +41,9 @@
public class CollectionUsageThreshold {
private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
- private static List pools = ManagementFactory.getMemoryPoolMXBeans();
- private static List managers = ManagementFactory.getMemoryManagerMXBeans();
- private static Map result = new HashMap();
+ private static List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
+ private static List<MemoryManagerMXBean> managers = ManagementFactory.getMemoryManagerMXBeans();
+ private static Map<String, PoolRecord> result = new HashMap<>();
private static boolean trace = false;
private static boolean testFailed = false;
private static final int EXPECTED_NUM_POOLS = 2;
@@ -119,8 +119,7 @@
}
// Find the Old generation which supports low memory detection
- for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {
- MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
+ for (MemoryPoolMXBean p : pools) {
MemoryUsage u = p.getUsage();
if (p.isUsageThresholdSupported() && p.isCollectionUsageThresholdSupported()) {
PoolRecord pr = new PoolRecord(p);
@@ -134,25 +133,30 @@
throw new RuntimeException("Unexpected number of selected pools");
}
- checker = new Checker("Checker thread");
- checker.setDaemon(true);
- checker.start();
+ try {
+ checker = new Checker("Checker thread");
+ checker.setDaemon(true);
+ checker.start();
+
+ for (PoolRecord pr : result.values()) {
+ pr.getPool().setCollectionUsageThreshold(THRESHOLD);
+ System.out.println("Collection usage threshold of " +
+ pr.getPool().getName() + " set to " + THRESHOLD);
+ }
- for (Iterator iter = result.values().iterator(); iter.hasNext();) {
- PoolRecord pr = (PoolRecord) iter.next();
- pr.getPool().setCollectionUsageThreshold(THRESHOLD);
- System.out.println("Collection usage threshold of " +
- pr.getPool().getName() + " set to " + THRESHOLD);
- }
+ SensorListener listener = new SensorListener();
+ NotificationEmitter emitter = (NotificationEmitter) mm;
+ emitter.addNotificationListener(listener, null, null);
- SensorListener listener = new SensorListener();
- NotificationEmitter emitter = (NotificationEmitter) mm;
- emitter.addNotificationListener(listener, null, null);
-
- mm.setVerbose(true);
- for (int i = 0; i < NUM_GCS; i++) {
- invokeGC();
- checker.waitForCheckResult();
+ for (int i = 0; i < NUM_GCS; i++) {
+ invokeGC();
+ checker.waitForCheckResult();
+ }
+ } finally {
+ // restore the default
+ for (PoolRecord pr : result.values()) {
+ pr.getPool().setCollectionUsageThreshold(0);
+ }
}
if (testFailed)
@@ -168,8 +172,7 @@
mm.gc();
if (trace) {
- for (Iterator iter = result.values().iterator(); iter.hasNext();) {
- PoolRecord pr = (PoolRecord) iter.next();
+ for (PoolRecord pr : result.values()) {
System.out.println("Usage after GC for: " + pr.getPool().getName());
MemoryUtil.printMemoryUsage(pr.getPool().getUsage());
}
@@ -200,8 +203,7 @@
}
}
private void checkResult() {
- for (Iterator iter = result.values().iterator(); iter.hasNext();) {
- PoolRecord pr = (PoolRecord) iter.next();
+ for (PoolRecord pr : result.values()) {
if (pr.getListenerInvokedCount() != numGCs) {
throw new RuntimeException("Listeners invoked count = " +
pr.getListenerInvokedCount() + " expected to be " +