Java JMX API has very simple API to find out the memory usage of a JVM instance. In the below given example we will find the memory usage of a JVM instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.ourownjava.corejava.util; import java.lang.management.ManagementFactory; import java.lang.management.MemoryUsage; /** * * @author ourownjava.com * @date 28th June, 2011 * * Example program to find the memory usage of JVM * */ public class HeapMemoryUsageExample { public static void main(String args[]){ final MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean(). getHeapMemoryUsage(); System.out.println("Allocated memory for JVM :"+heapMemoryUsage.getCommitted()); System.out.println("Initially requested memory :"+heapMemoryUsage.getInit()); System.out.println("Maximum memory can be used :"+heapMemoryUsage.getMax()); System.out.println("Memory used by the JVM :"+heapMemoryUsage.getUsed()); } } |
Console output
1 2 3 4 5 6 |
/mnt/opt/java com.ourownjava.corejava.util.HeapMemoryUsageExample Allocated memory for JVM :5177344 Initially requested memory :0 Maximum memory can be used :66650112 Memory used by the JVM :205096 |