Search code examples
garbage-collectionjvmjmxheap-dump

how can I know size of each generation in java heap with jmx


jmap can know size of each generation, but I want to monitor my java process real time.

With jmx, MemoryMXBean.getHeapMemoryUsage().getUsed() can get the total heap size. But I cannot find any method to get:

  1. Size of New (Young) and Tenured (Old) generations;
  2. Size of Eden and each Survivor in New generations.

Solution

  • You can use ManagementFactory.getMemoryPoolMXBeans(). It shows all the memory regions. Depending on the GC you used, the names differ. pool.getCollectionUsage().getInit() gives the initial size of a pool. pool.getName() is for example 'G1 Eden Space' or 'G1 Old Gen' or 'G1 Survivor Space' if you use G1 GC.

    pool.getUsage().getUsed() returns as well the amount of used memory for that region.