Hi,

Jay SenSharma

Jay SenSharma

If we want to analyze the Heap & Non-Heap Usages of WebLogic Server JVM remotely then we can use the following Simple JMX Code… You can enhance this tiny code to Develop your own Mini/Light weight JConsole. To connect to the Actual JConsole remotely  also you need to provide the following JAVA_OPTIONS and the connection url format should be:

service:jmx:rmi:///jndi/rmi://”+hostname+”:”+port+”/jmxrmi

Step1). Please open the “startWebLogic.cmd” (Server StartScript) and then set the following JAVA_OPTIONS there.

set MY_JMX_OPTIONS= -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=4444 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false
set JAVA_OPTIONS=%JAVA_OPTIONS%  %MY_JMX_OPTIONS%

Step2). Now from any other box or from the same Box you can run the Following JMX code to get Heap & Non Heap Usages of the JVM:
“HeapAndNonHeapMonitoring.java”


import java.util.Hashtable;
import java.io.*;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import java.lang.management.MemoryMXBean;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import javax.management.*;
import javax.management.openmbean.CompositeDataSupport;

public class HeapAndNonHeapMonitoring
{
private static MBeanServerConnection connection;
private static JMXConnector connector;
public static void Connection(String hostname, String port) throws IOException
{
Integer portInteger = Integer.valueOf(port);
Hashtable h = new Hashtable();
JMXServiceURL address = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+hostname+":"+port+"/jmxrmi");
connector = JMXConnectorFactory.connect(address,null);
connection = connector.getMBeanServerConnection();
System.out.println("GOT THE MBeanServerConnection---SUCCESSFULLY");
}

private static void doGarbageCollection() throws Exception
{
ObjectName memoryMXBean=new ObjectName("java.lang:type=Memory");
connection.invoke(memoryMXBean,"gc", null, null);
System.out.println("nnt------Garbage Collection Done Successfully-----");
}

private static void doOperatingSystemDetails() throws Exception
{
ObjectName operatingSystemMXBean=new ObjectName("java.lang:type=OperatingSystem");
Object systemLoadAverage = connection.getAttribute(operatingSystemMXBean, "SystemLoadAverage");
Long freePhysicalMemory = (Long) connection.getAttribute(operatingSystemMXBean, "FreePhysicalMemorySize");
Long processCpuTime = (Long) connection.getAttribute(operatingSystemMXBean, "ProcessCpuTime");

Long committedVirtualMemorySize = (Long) connection.getAttribute(operatingSystemMXBean, "CommittedVirtualMemorySize");
Long freeSwapSpaceSize = (Long) connection.getAttribute(operatingSystemMXBean, "FreeSwapSpaceSize");
Long totalPhysicalMemorySize = (Long) connection.getAttribute(operatingSystemMXBean, "TotalPhysicalMemorySize");
Long totalSwapSpaceSize = (Long) connection.getAttribute(operatingSystemMXBean, "TotalSwapSpaceSize");
System.out.println("Operating SystemLoadAverage: " + systemLoadAverage);
System.out.println("Operating System FreePhysicalMemory: " + (freePhysicalMemory/(1024*1024))+"-MB");
System.out.println("Operating System processCpuTime: " + processCpuTime);
System.out.println("Operating System committedVirtualMemorySize: " + (committedVirtualMemorySize/(1024*1024))+"-MB");
System.out.println("Operating System freeSwapSpaceSize: " + (freeSwapSpaceSize/(1024*1024))+"-MB");
System.out.println("Operating System totalPhysicalMemorySize: " + (totalPhysicalMemorySize/(1024*1024))+"-MB");
System.out.println("Operating System totalSwapSpaceSize: " + (totalSwapSpaceSize/(1024*1024))+"-MB");
}

private static void getHeapMemoryUsage() throws Exception
{
ObjectName memoryMXBean=new ObjectName("java.lang:type=Memory");
CompositeDataSupport dataSenders = (CompositeDataSupport) connection.getAttribute(memoryMXBean,"HeapMemoryUsage");
if (dataSenders != null)
{
Long commited = (Long) dataSenders.get("committed");
Long init = (Long) dataSenders.get("init");
Long max = (Long) dataSenders.get("max");
Long used = (Long) dataSenders.get("used");
Long percentage = ((used * 100) / max);
System.out.println("nnt commited   : "+commited/(1024*1024)+" MB");
System.out.println("t init       : "+init/(1024*1024)+" MB");
System.out.println("t max        : "+max/(1024*1024)+" MB");
System.out.println("t used       : "+used/(1024*1024)+" MB");
System.out.println("t percentage : "+percentage +" %");
}
}

private static void getNonHeapMemoryUsage() throws Exception
{
ObjectName memoryMXBean=new ObjectName("java.lang:type=Memory");
CompositeDataSupport dataSenders = (CompositeDataSupport) connection.getAttribute(memoryMXBean,"NonHeapMemoryUsage");
if (dataSenders != null)
{
Long commited = (Long) dataSenders.get("committed");
Long init = (Long) dataSenders.get("init");
Long max = (Long) dataSenders.get("max");
Long used = (Long) dataSenders.get("used");
Long percentage = ((used * 100) / max);
System.out.println("nnt commited   : "+commited/(1024*1024)+" MB");
System.out.println("t init       : "+init/(1024*1024)+" MB");
System.out.println("t max        : "+max/(1024*1024)+" MB");
System.out.println("t used       : "+used/(1024*1024)+" MB");
System.out.println("t percentage : "+percentage +" %");
}
}

public static void main(String[] args) throws Exception
{
String hostname = args[0];
String port = args[1];
Connection(hostname, port);
//doGarbageCollection();                // --> use this method if you want to perform Garbage Collection
System.out.println("nt----------HEAP Memory Usages---------");
getHeapMemoryUsage();
System.out.println("nt----------Non-HEAP Memory Usages---------");
getNonHeapMemoryUsage();
System.out.println("nt----------Operating System Usages---------");
doOperatingSystemDetails();
connector.close();
}
}

Output:

Operating_System_Usages_JMX

Operating_System_Usages_JMX

.

.

.

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.