Well, JDK 1.5 has some fancy new stuff that's really nifty.
First off, you get a good picture of what your memory looks like (both heap and non-heap) like so:
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
MemoryUsage heap = memBean.getHeapMemoryUsage();
MemoryUsage nonHeap = memBean.getNonHeapMemoryUsage();
System.out.println(heap);
System.out.println(nonHeap);
This gets you some fairly cool (and very bash-script analyzable) data:
init = 33161792(32384K) used = 301960(294K)
committed = 33226752(32448K) max = 512950272(500928K)
init = 19136512(18688K) used = 1913488(1868K)
committed = 19136512(18688K) max = 117440512(114688K)
Now, on to more interactive things:
public class Mem implements NotificationListener {
public void handleNotification(Notification n, Object hb) {
//-- we'll get to this in a bit
}
public static void main (String[] args) {
Mem mem = new Mem();
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
NotificationEmitter ne = (NotificationEmitter)memBean ;
ne.addNotificationListener(mem, null, null);
//-- more to come: configure thresholds
}
}
The above class implements the javax.management.NotificationListener interface that defines the handleNotification method. Now, the main method fetches the MemoryMXBean which turns out to also be a NotificationEmitter (javadocs come in real handy). You could actually set up notifications where you define both filters and user objects can be applied and handed into the handleNotification call. We'll be amateurs and opt to filter nothing and not forward any of our objects.
With the notification mechanism in place, we need set up a threshold to be notified on. Expanding the main method:
public static void main (String[] args) {
Mem mem = new Mem();
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
NotificationEmitter ne = (NotificationEmitter)memBean ;
ne.addNotificationListener(mem, null, null);
ListmemPools = ManagementFactory.getMemoryPoolMXBeans();
for (Iteratori = memPools.iterator(); i.hasNext();) {
MemoryPoolMXBean mp = i.next();
if (mp.isUsageThresholdSupported() ) {
// Found the heap! Let's add a notifier
MemoryUsage mu = mp.getUsage();
long max = mu.getMax();
long alert = (max * 50)/100;
System.out.println("Setting a warning on pool: " + mp.getName() + " for: " + alert);
mp.setUsageThreshold(alert);
}
}
We can call for all the MemoryPoolMXBeans, and then check to see if threshold setting is supported. For those that we can configure, we set an alert for when we surpass 50% usage. At this point, you are in some pretty rarefied territory within the VM:
Setting a warning on pool: Code Cache for: 25165824
Setting a warning on pool: PS Old Gen for: 236748800
Setting a warning on pool: PS Perm Gen for: 33554432
So, can we avert the dreaded out of memory condition?
Here's our uber-malicious memory leak:
import java.util.*;
public class Leak extends Thread {
public static boolean keepLeaking = true;
public void run () {
Mapmap = new HashMap ();
int x = 0;
while (keepLeaking) {
String key = new String(""+x*10000);
String value = new String (""+x*243523);
map.put(key, value);
try { Thread.sleep(1); } catch (Exception e) {}
x++;
}
}
}
A call to set keepLeaking to false can avert a sure VM memory issue. So, here's the fleshed out handleNotification method:
public void handleNotification(Notification n, Object hb) {
String type = n.getType();
if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
// retrieve the memory notification information
CompositeData cd = (CompositeData) n.getUserData();
MemoryNotificationInfo memInfo = MemoryNotificationInfo.from(cd);
System.out.println(memInfo.getPoolName() + " has exceeded the threshold : " +
memInfo.getCount() + " times");
System.out.println(memInfo.getUsage());
Leak.keepLeaking = false;
} else {
System.out.println("Unknown notification: " + n);
}
}
And, presto:
$ java -Xmx8m Mem
Setting a warning on pool: Code Cache for: 25165824
Setting a warning on pool: PS Old Gen for: 3735552
Setting a warning on pool: PS Perm Gen for: 33554432
Leaks started
PS Old Gen has exceeded the threshold : 1 times
init = 1441792(1408K) used = 3763520(3675K) committed = 4849664(4736K) max = 7471104(7296K)
$
You'd probably want to reset the peak usage a few times and ensure that you see a consistent incursion before doing anything dramatic (give the GC a chance..). But, this is a fairly large leap from the prior polling mechanism that we would have had to adopt to monitor how a VM is doing. Now, we can code the reaction to a situation with set thresholds instead of managing polling threads.