Hi,

Jay SenSharma

Jay SenSharma

Here is a Simple java Program which can be used to get the CPU, FileSystem and JVM memory details… This program can be set as a Start up class of WebLogic Server…or it can be deployed as a Monitoring application on Weblogic Server sothat whenever we want to see these details we can run this program from inside the WebLogic Container…As a Scheduler program or as a Simple WebApplication.

This is a very Simple code which can be enhanced more…Using the same API which is used in the following JMX Code.

import java.io.*;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;

public class CPU_And_Memory_Analysis
{
private static void printUsage() {
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);
}
}
}


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-------------");
printUsage();
}
}

===============================

OUTPUT:

C:DELETE>java CPU_And_Memory_Analysis

—————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               : 132875 MB
Usable Space             : 132875 MB

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

—————CPU USAGES————-

getCommittedVirtualMemorySize = 20668416
getFreePhysicalMemorySize = 545689600
getFreeSwapSpaceSize = 1672167424
getProcessCpuTime = 93750000
getTotalPhysicalMemorySize = 1876234240
getTotalSwapSpaceSize = 3862908928

C:DELETE>java -Xms512m -Xmx1024m CPU_And_Memory_Analysis

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

—————FileSystem Details————-
FileSystem Root Details: C:
Total Space              : 152625 MB
Free Space               : 132875 MB
Usable Space             : 132875 MB

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

—————CPU USAGES————-

getCommittedVirtualMemorySize = 556224512
getFreePhysicalMemorySize = 543604736
getFreeSwapSpaceSize = 1136537600
getProcessCpuTime = 78125000
getTotalPhysicalMemorySize = 1876234240
getTotalSwapSpaceSize = 3862908928

.

.

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.