How do I determine the size of each of the heap generations in my JVM?

heap-memory, java, jvm

Solution

I'm presuming you mean determining this programmatically. You should take a peek at `java.lang.management`. In particular, `ManagementFactory.getMemoryPoolMXBeans` should be useful here.

final List<MemoryPoolMXBeans> pools = ManagementFactory.getMemoryPoolMXBeans();
for (final MemoryPoolMXBean pool : pools) {
  if (pool.getType() == MemoryType.HEAP) {
    final String name = pool.getName();
    final MemoryUsage usage = pool.getUsage();
    if (name.startsWith("Eden")) {
      System.out.println("found eden: " + usage);
    } else if (name.startsWith("Tenured")) {
      System.out.println("found tenured: " + usage);
    }
  }
}

This should work using the HotSpot JVM.

If you're not seeking to do this programmatically, VisualVM (`jvisualvm` in the JDK) allows you to monitor heap usage amongst many other nifty things.

(source: java.net)

Note, however, that VisualVM lacks support of monitoring specific memory pools out-of-the-box; you'll need to install jvmstat. You must then install the Visual GC plugin from VisualVM's plugin manager.

http://puu.sh/YCpX

After installing and restarting Visual VM, we can see a more in-depth view of heap usage via the Visual GC tab.

http://puu.sh/YCqD

Problem

If I start my JVM with -Xms256M and -Xmx512M, at any point of time, can I determine the amount of space specifically allocated to the young and tenured generations of the heap ?

Original source