Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 571
Parag Avatar asked Aug 25 '12 07:08

Parag


Video Answer


2 Answers

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

like image 60
obataku Avatar answered Oct 02 '22 08:10

obataku


Use JConsole to easily monitor the heap sizes. You can see different gen sizes in the memory tab. Select the gen you want from the Chart drop down.

like image 45
maneesh Avatar answered Oct 02 '22 10:10

maneesh