Hi,

Jay SenSharma

Jay SenSharma

Here is a Simple Demonstration of Using The  OperatingSystemMXBean, MemoryMXBean, MemoryPoolMXBean and GarbageCollectorMXBean. To Monitor the Heap and Non-Heap Memory Usages like PermGen Space, EdenSpace, Survivor Space and Tenured Gen.

Just run the Following Java Program which uses the above MxBeans.  There are few more MxBean which u can use in the same Manner: http://java.sun.com/j2se/1.5.0/docs/guide/management/overview.html#mxbeans

JVM_Mbeans_1.java

import java.io.*;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryMXBean;
public class JVM_Mbeans_1
{

private static void printOperatingSystemMXBean()
{
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
System.out.print("n");
for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods())
{
method.setAccessible(true);
if (method.getName().startsWith("get")&& Modifier.isPublic(method.getModifiers()))
{
Object value;
try
{
value = method.invoke(operatingSystemMXBean);
}
catch (Exception e)
{
value = e;
e.printStackTrace();
}
System.out.println("t"+method.getName() + " = " + value);
}
}
}

private static void printGarbageCollectorMXBean()
{
java.util.List<java.lang.management.GarbageCollectorMXBean> list=(java.util.List<java.lang.management.GarbageCollectorMXBean>) ManagementFactory.getGarbageCollectorMXBeans();
int count=0;
for(GarbageCollectorMXBean temp: list)
{
count++;
GarbageCollectorMXBean garbageCollectorMXBean = temp;
System.out.println("n********MemoryManager: "+garbageCollectorMXBean.getName());
String arr[]=garbageCollectorMXBean.getMemoryPoolNames();
for (String name:arr)
{
System.out.println("nt GarbageCollectorMXBean-"+(count)+"tMemory Pool names: "+name);
}
}
}

private static void printMemoryPoolMXBean()
{
java.util.List<java.lang.management.MemoryPoolMXBean> list=(java.util.List<java.lang.management.MemoryPoolMXBean>) ManagementFactory.getMemoryPoolMXBeans();
int count=0;
for(MemoryPoolMXBean temp: list)
{
count++;
MemoryPoolMXBean memoryPoolMXBean = temp;
System.out.println("n********MemoryManager: "+memoryPoolMXBean.getName());

for (Method method : memoryPoolMXBean.getClass().getDeclaredMethods())
{
method.setAccessible(true);

boolean collectionUsageThresholdSupported=memoryPoolMXBean.isCollectionUsageThresholdSupported();
boolean usageThresholdSupported=memoryPoolMXBean.isUsageThresholdSupported();

//if (collectionUsageThresholdSupported==true & usageThresholdSupported==true)

String methodName=method.getName();   //System.out.println("=========================> "+methodName);;

if (method.getName().startsWith("get")&& Modifier.isPublic(method.getModifiers()))
{
Object value=new Object();;
try
{
if(!usageThresholdSupported==false  && !collectionUsageThresholdSupported==false)
value = method.invoke(memoryPoolMXBean);
}
catch (Exception e)
{
value = e;
e.printStackTrace();
}
System.out.println("t"+method.getName() + " = " + value);
}
}
}
}

private static void printMemoryMXBean()
{
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
System.out.print("n");
for (Method method : memoryMXBean.getClass().getDeclaredMethods())
{
method.setAccessible(true);
if (method.getName().startsWith("get")&& Modifier.isPublic(method.getModifiers()))
{
Object value;
try
{
value = method.invoke(memoryMXBean);
}
catch (Exception e)
{
value = e;
e.printStackTrace();
}
System.out.println("t"+method.getName() + " = " + value);
}
}
}

public static void main(String[] args)
{
System.out.println("nnt----------JVM Runtime Details---------");
System.out.println("tAvailable processors (Cores): " +   Runtime.getRuntime().availableProcessors());
System.out.println("tInitial Memory (-Xms)       : " +  (Runtime.getRuntime().freeMemory()/(1024*1024))+" MB");
long maxMemory = Runtime.getRuntime().maxMemory() ;
System.out.println("tMaximum JVM Memory (-Xmx)   : " + (maxMemory/(1024*1024))+" MB");
System.out.println("tTotal Used JVM Memory       : " + (Runtime.getRuntime().totalMemory()/(1024*1024))+" MB");

File[] roots = File.listRoots();
System.out.println("nnt----------FileSystem Details---------");
for (File root : roots)
{
System.out.println("ntFileSystem Root Details: " + root.getAbsolutePath());
System.out.println("tTotal Space              : " + (root.getTotalSpace()/(1024*1024))+" MB");
System.out.println("tFree Space               : " + (root.getFreeSpace()/(1024*1024))+" MB");
System.out.println("tUsable Space             : " + (root.getUsableSpace()/(1024*1024))+" MB");
}
System.out.println("nnt----------CPU USAGES---------");
printOperatingSystemMXBean();

System.out.println("nnt----------GARBAGE COLLECTOR USAGES---------");
printGarbageCollectorMXBean();

System.out.println("nnt----------MemoryPoolMXBean USAGES---------");
printMemoryPoolMXBean();

System.out.println("nnt----------MemoryMXBean USAGES---------");
printMemoryMXBean();
}
}

Output Below In Sun JDK

C:DELETEJVM_MBeans>java JVM_Mbeans_1

———-JVM Runtime Details———
Available processors (Cores): 2
Initial Memory (-Xms)       : 4 MB
Maximum JVM Memory (-Xmx)   : 63 MB
Total Used JVM Memory       : 4 MB

———-FileSystem Details———

FileSystem Root Details: C:
Total Space              : 152625 MB
Free Space               : 132617 MB
Usable Space             : 132617 MB

FileSystem Root Details: D:
Total Space              : 0 MB
Free Space               : 0 MB
Usable Space             : 0 MB

———-CPU USAGES———

getCommittedVirtualMemorySize = 20668416
getFreePhysicalMemorySize = 625307648
getFreeSwapSpaceSize = 2282778624
getProcessCpuTime = 78125000
getTotalPhysicalMemorySize = 1876234240
getTotalSwapSpaceSize = 3862908928

———-GARBAGE COLLECTOR USAGES———

********MemoryManager: Copy

GarbageCollectorMXBean-1       Memory Pool names: Eden Space

GarbageCollectorMXBean-1       Memory Pool names: Survivor Space

********MemoryManager: MarkSweepCompact

GarbageCollectorMXBean-2       Memory Pool names: Eden Space

GarbageCollectorMXBean-2       Memory Pool names: Survivor Space

GarbageCollectorMXBean-2       Memory Pool names: Tenured Gen

GarbageCollectorMXBean-2       Memory Pool names: Perm Gen

———-MemoryPoolMXBean USAGES———

********MemoryManager: Code Cache
getName = java.lang.Object@1186fab
getType = java.lang.Object@14b7453
getCollectionUsage = java.lang.Object@c21495
getCollectionUsageThreshold = java.lang.Object@1d5550d
getCollectionUsageThresholdCount = java.lang.Object@a0dcd9
getMemoryManagerNames = java.lang.Object@15f5897
getPeakUsage = java.lang.Object@1cfb549
getUsage = java.lang.Object@1820dda
getUsageThreshold = java.lang.Object@87816d
getUsageThresholdCount = java.lang.Object@1d9dc39

********MemoryManager: Eden Space
getName = java.lang.Object@93dcd
getType = java.lang.Object@b89838
getCollectionUsage = java.lang.Object@111a3ac
getCollectionUsageThreshold = java.lang.Object@110b053
getCollectionUsageThresholdCount = java.lang.Object@a83b8a
getMemoryManagerNames = java.lang.Object@dd20f6
getPeakUsage = java.lang.Object@19efb05
getUsage = java.lang.Object@723d7c
getUsageThreshold = java.lang.Object@22c95b
getUsageThresholdCount = java.lang.Object@1d1acd3

********MemoryManager: Survivor Space
getName = java.lang.Object@a981ca
getType = java.lang.Object@8814e9
getCollectionUsage = java.lang.Object@1503a3
getCollectionUsageThreshold = java.lang.Object@1a1c887
getCollectionUsageThresholdCount = java.lang.Object@743399
getMemoryManagerNames = java.lang.Object@e7b241
getPeakUsage = java.lang.Object@167d940
getUsage = java.lang.Object@e83912
getUsageThreshold = java.lang.Object@1fae3c6
getUsageThresholdCount = java.lang.Object@7ffe01

********MemoryManager: Tenured Gen
getName = Tenured Gen
getType = Heap memory
getCollectionUsage = init = 4194304(4096K) used = 0(0K) committed = 0(0K) max = 61997056(60544K)
getCollectionUsageThreshold = 0
getCollectionUsageThresholdCount = 0
getMemoryManagerNames = [Ljava.lang.String;@fd13b5
getPeakUsage = init = 4194304(4096K) used = 0(0K) committed = 4194304(4096K) max = 61997056(60544K)
getUsage = init = 4194304(4096K) used = 0(0K) committed = 4194304(4096K) max = 61997056(60544K)
getUsageThreshold = 0
getUsageThresholdCount = 0

********MemoryManager: Perm Gen
getName = Perm Gen
getType = Non-heap memory
getCollectionUsage = init = 12582912(12288K) used = 0(0K) committed = 0(0K) max = 67108864(65536K)
getCollectionUsageThreshold = 0
getCollectionUsageThresholdCount = 0
getMemoryManagerNames = [Ljava.lang.String;@118f375
getPeakUsage = init = 12582912(12288K) used = 1827536(1784K) committed = 12582912(12288K) max = 67108864(65536K)
getUsage = init = 12582912(12288K) used = 1827536(1784K) committed = 12582912(12288K) max = 67108864(65536K)
getUsageThreshold = 0
getUsageThresholdCount = 0

———-MemoryMXBean USAGES———

getHeapMemoryUsage = init = 0(0K) used = 313520(306K) committed = 5177344(5056K) max = 66650112(65088K)
getNonHeapMemoryUsage = init = 12779520(12480K) used = 2356888(2301K) committed = 13107200(12800K) max = 100663296(98304K)
getNotificationInfo = [Ljavax.management.MBeanNotificationInfo;@1dd7056
getObjectPendingFinalizationCount = 0

———————————————————

Output Below In JRockit JDK

———————————————————

C:DELETEJVM_MBeans>java JVM_Mbeans_1

———-JVM Runtime Details———
Available processors (Cores): 2
Initial Memory (-Xms)       : 58 MB
Maximum JVM Memory (-Xmx)   : 1024 MB
Total Used JVM Memory       : 64 MB

———-FileSystem Details———

FileSystem Root Details: C:
Total Space              : 152625 MB
Free Space               : 132617 MB
Usable Space             : 132617 MB

FileSystem Root Details: D:
Total Space              : 0 MB
Free Space               : 0 MB
Usable Space             : 0 MB

———-CPU USAGES———

getCommittedVirtualMemorySize = 86503424
getTotalSwapSpaceSize = 3862908928
getFreeSwapSpaceSize = 2215833600
getProcessCpuTime = 343750000
getFreePhysicalMemorySize = 592637952
getTotalPhysicalMemorySize = 1876234240

———-GARBAGE COLLECTOR USAGES———

********MemoryManager: Garbage collection optimized for throughput Young Collector

GarbageCollectorMXBean-1       Memory Pool names: Old Space

GarbageCollectorMXBean-1       Memory Pool names: Nursery

********MemoryManager: Garbage collection optimized for throughput Old Collector

GarbageCollectorMXBean-2       Memory Pool names: Old Space

GarbageCollectorMXBean-2       Memory Pool names: Nursery

———-MemoryPoolMXBean USAGES———

********MemoryManager: Nursery
getName = Nursery
getType = Heap memory
getUsage = init = 4294967295(4194303K) used = 2424832(2368K) committed = 33554432(32768K) max = 1020054732(996147K)
getPeakUsage = init = 4294967295(4194303K) used = 2424832(2368K) committed = 33554432(32768K) max = 1020054732(996147
K)
getUsageThreshold = 0
getMemoryManagerNames = [Ljava.lang.String;@18b358
getUsageThresholdCount = 0
getCollectionUsageThreshold = 0
getCollectionUsageThresholdCount = 0
getCollectionUsage = init = 4294967295(4194303K) used = 0(0K) committed = 0(0K) max = 0(0K)

********MemoryManager: Old Space
getName = Old Space
getType = Heap memory
getUsage = init = 67108864(65536K) used = 6111192(5967K) committed = 67108864(65536K) max = 1073741824(1048576K)
getPeakUsage = init = 67108864(65536K) used = 6111192(5967K) committed = 67108864(65536K) max = 1073741824(1048576K)
getUsageThreshold = 0
getMemoryManagerNames = [Ljava.lang.String;@18ba3b
getUsageThresholdCount = 0
getCollectionUsageThreshold = 0
getCollectionUsageThresholdCount = 0
getCollectionUsage = init = 67108864(65536K) used = 0(0K) committed = 0(0K) max = 0(0K)

********MemoryManager: Class Memory
getName = Class Memory
getType = Non-heap memory
getUsage = init = 524288(512K) used = 2129172(2079K) committed = 2359296(2304K) max = 4294967295(4194303K)
getPeakUsage = init = 524288(512K) used = 2129172(2079K) committed = 2359296(2304K) max = 4294967295(4194303K)
getUsageThreshold = 0
getMemoryManagerNames = [Ljava.lang.String;@18c0e7
getUsageThresholdCount = 0
getCollectionUsageThreshold = 0
getCollectionUsageThresholdCount = 0
getCollectionUsage = null

———-MemoryMXBean USAGES———

getObjectPendingFinalizationCount = 0
getHeapMemoryUsage = init = 4294967295(4194303K) used = 6111192(5967K) committed = 67108864(65536K) max = 1073741824(
1048576K)
getNonHeapMemoryUsage = init = 524288(512K) used = 2161412(2110K) committed = 2359296(2304K) max = 4294967295(4194303
K)
getNotificationInfo = [Ljavax.management.MBeanNotificationInfo;@18c812

.

.

Thanks

Jay SenSharma

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.