retrieving CPU Usage and Memory Usage information in JAVA

H

hvt

Hi All,
I 've been searching through this group for information on CPU and
Memory usage retrieval on Windows machine using JAVA. After following
so many previous threads on the same subject, the possible solution I
've observed was using JNI calls to native libraries for these OS
specific information retrieval.
Later I found that java management libraries already has it, native
libraries and some jmx classes.

Sharing the code, hope this may help group members in their
requirements.

Code:
com.sun.management.OperatingSystemMXBean mxbean =

(com.sun.management.OperatingSystemMXBean)
ManagementFactory.getOperatingSystemMXBean();

System.out.println("********* CPU & MEMORY USAGE OF \\harsh
*********");
System.out.println("OS : "+mxbean.getName());
System.out.println("Available Processors :
"+mxbean.getAvailableProcessors());
System.out.println("Commited Virtual Memory :
"+mxbean.getCommittedVirtualMemorySize());
System.out.println("Free Physical Memory :
"+mxbean.getFreePhysicalMemorySize());
System.out.println("Free Swap Space Size :
"+mxbean.getFreeSwapSpaceSize());
System.out.println("Process CPU Time : "+mxbean.getProcessCpuTime());
System.out.println("Total Physical Memory Size :
"+mxbean.getTotalPhysicalMemorySize());
System.out.println("Total Swap Space Size :
"+mxbean.getTotalSwapSpaceSize());

Problem:
getProcessCpuTime() method returns the CPU time used by the process on
which the Java virtual machine is running NOT the '%CPU Usage' time
which i was hoping to get.

If anyone is having idea about how to obtain '%CPU Usage' of windows
machine using java program, kindly post the message.

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top