--- a/jdk/test/java/lang/management/MemoryMXBean/LowMemoryTest.java Thu Dec 19 18:13:19 2013 +0000
+++ b/jdk/test/java/lang/management/MemoryMXBean/LowMemoryTest.java Thu Dec 19 13:43:30 2013 -0800
@@ -32,17 +32,21 @@
*
* @build LowMemoryTest MemoryUtil
* @run main/othervm/timeout=600 LowMemoryTest
+ * @run main/othervm/timeout=600 -XX:+UseConcMarkSweepGC LowMemoryTest
+ * @run main/othervm/timeout=600 -XX:+UseParallelGC LowMemoryTest
+ * @run main/othervm/timeout=600 -XX:+UseSerialGC LowMemoryTest
*/
import java.lang.management.*;
import java.util.*;
+import java.util.concurrent.Phaser;
import javax.management.*;
import javax.management.openmbean.CompositeData;
public class LowMemoryTest {
- private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
- private static List pools = ManagementFactory.getMemoryPoolMXBeans();
- private static List managers = ManagementFactory.getMemoryManagerMXBeans();
+ private static final MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
+ private static final List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
+ private static final Phaser phaser = new Phaser(2);
private static MemoryPoolMXBean mpool = null;
private static boolean trace = false;
private static boolean testFailed = false;
@@ -50,8 +54,9 @@
private static final int NUM_CHUNKS = 2;
private static long chunkSize;
- private static boolean listenerInvoked = false;
+ private static volatile boolean listenerInvoked = false;
static class SensorListener implements NotificationListener {
+ @Override
public void handleNotification(Notification notif, Object handback) {
String type = notif.getType();
if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
@@ -69,8 +74,9 @@
static class TestListener implements NotificationListener {
private int triggers = 0;
- private long[] count = new long[NUM_TRIGGERS * 2];
- private long[] usedMemory = new long[NUM_TRIGGERS * 2];
+ private final long[] count = new long[NUM_TRIGGERS * 2];
+ private final long[] usedMemory = new long[NUM_TRIGGERS * 2];
+ @Override
public void handleNotification(Notification notif, Object handback) {
MemoryNotificationInfo minfo = MemoryNotificationInfo.
from((CompositeData) notif.getUserData());
@@ -148,15 +154,20 @@
newThreshold);
}
+
allocator.start();
+ // Force Allocator start first
+ phaser.arriveAndAwaitAdvance();
sweeper.start();
+
try {
allocator.join();
+ // Wait until AllocatorThread's done
+ phaser.arriveAndAwaitAdvance();
sweeper.join();
} catch (InterruptedException e) {
- e.printStackTrace();
- System.out.println("Unexpected exception.");
+ System.out.println("Unexpected exception:" + e);
testFailed = true;
}
@@ -173,45 +184,17 @@
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
- e.printStackTrace();
- System.out.println("Unexpected exception.");
+ System.out.println("Unexpected exception:" + e);
testFailed = true;
}
}
- private static Object go = new Object();
- private static boolean waiting = false; // No thread is waiting.
-
- // Synchronizes two thread. If no thread is waiting then wait
- // for notification from a different thread and if it is
- // is waiting then send notification.
- // In this test case this method is used to synchronize sweeper
- // thread and alocater thread to reach a particular point.
- private static void wait_or_notify() {
- synchronized (go) {
- if (waiting == false) {
- waiting = true;
- System.out.println(" Waiting ");
- try {
- go.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- testFailed = true;
- }
- waiting = false;
- } else {
- System.out.println(" Notify ");
- go.notify();
- }
- }
- }
-
- private static List objectPool = new ArrayList();
+ private static final List<Object> objectPool = new ArrayList<>();
static class AllocatorThread extends Thread {
public void doTask() {
int iterations = 0;
int numElements = (int) (chunkSize / 4); // minimal object size
- while (!listenerInvoked) {
+ while (!listenerInvoked || mpool.getUsage().getUsed() < mpool.getUsageThreshold()) {
iterations++;
if (trace) {
System.out.println(" Iteration " + iterations +
@@ -234,23 +217,25 @@
goSleep(100);
}
}
+ @Override
public void run() {
for (int i = 1; i <= NUM_TRIGGERS; i++) {
- System.out.println("AllocatorThread is doing task " + i);
+ // Sync with SweeperThread's second phase.
+ phaser.arriveAndAwaitAdvance();
+ System.out.println("AllocatorThread is doing task " + i +
+ " phase " + phaser.getPhase());
doTask();
- synchronized (sweep) {
- sweep.notify();
+ // Sync with SweeperThread's first phase.
+ phaser.arriveAndAwaitAdvance();
+ System.out.println("AllocatorThread done task " + i +
+ " phase " + phaser.getPhase());
+ if (testFailed) {
+ return;
}
- // System.out.print(" Allocater Thread ");
- // If sweeper thread is waiting then send notify
- // else wait for notification from sweeper thread.
- wait_or_notify();
- if (testFailed) return;
}
}
}
- private static Object sweep = new Object();
static class SweeperThread extends Thread {
private void doTask() {
for (; mpool.getUsage().getUsed() >=
@@ -261,28 +246,21 @@
goSleep(100);
}
}
+ @Override
public void run() {
for (int i = 1; i <= NUM_TRIGGERS; i++) {
- synchronized (sweep) {
- while (!listenerInvoked) {
- try {
- sweep.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- System.out.println("Unexpected exception.");
- testFailed = true;
- }
- }
- }
- System.out.println("SweepThread is doing task " + i);
+ // Sync with AllocatorThread's first phase.
+ phaser.arriveAndAwaitAdvance();
+ System.out.println("SweepThread is doing task " + i +
+ " phase " + phaser.getPhase());
doTask();
listenerInvoked = false;
- // System.out.print(" Sweeper Thread ");
- // If Allocater thread is waiting wait send notify
- // else wait for notfication from allocater thread.
- wait_or_notify();
+ // Sync with AllocatorThread's second phase.
+ phaser.arriveAndAwaitAdvance();
+ System.out.println("SweepThread done task " + i +
+ " phase " + phaser.getPhase());
if (testFailed) return;
}
}