11

I need an API to get CPU & memory usage of my current process or application in java.

I've got an API to get the CPU usage of the complete system but I need it for a particular process (getSystemCpuLoad of OperatingSystemMXBean interface)

Thanks in advance

2
  • 4
    It depends on what you mean by memory usage; do you mean resident memory, virtual memory, heap in use, live objects, These can be very different. Commented Apr 15, 2016 at 12:47
  • This can be obtained using Runtime. totalMemory() - freeMemory() note this doesn't give you much idea of how full the heap is, only how far you are from the next full GC. Commented Apr 19, 2016 at 8:29

3 Answers 3

8

You can obtain that data if you use a different OperatingSystemMXBean.

Check the imported package: com.sun.management.OperatingSystemMXBean.

 import java.lang.management.ManagementFactory;
 import com.sun.management.OperatingSystemMXBean;

 public class Test {

 public static void main(String[] args) {
       OperatingSystemMXBean operatingSystemMXBean = 
          (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
       System.out.println(operatingSystemMXBean.getProcessCpuLoad());
 }}

https://docs.oracle.com/javase/7/docs/jre/api/management/extension/com/sun/management/OperatingSystemMXBean.html

If I'm not mistaken, this class is included in rt.jar, present in your java runtime.

Sign up to request clarification or add additional context in comments.

5 Comments

Gives Compilation error : The method getProcessCpuLoad() is undefined for the type OperatingSystemMXBean.
@PSM Check the import. Are you sure that you are importing com.sun.management.OperatingSystemMXBean?
@PSM I think that you are still using java.lang.management.OperatingSystemMXBean
Is there a Java 8 solution? The above gives me the error The method getProcessCpuLoad() is undefined for the type OperatingSystemMXBean.
@CardinalSystem Have you checked the imported package? I've used it with Java 8
4

There is the good news and the bad news. The bad news is that programmatically querying for CPU usage is impossible using pure Java. There is simply no API for this. A suggested alternative might use Runtime.exec() to determine the JVM's process ID (PID), call an external, platform-specific command like ps, and parse its output for the PID of interest. But, this approach is fragile at best.

The good news, however, is that a reliable solution can be accomplished by stepping outside Java and writing a few C code lines that integrate with the Java application via Java Native Interface (JNI).

1 Comment

If you're going to copy and paste from infoworld.com/article/2077361/… you should at least give a link so that the reader can look there for further information.
0

Once you have the pid, you can use jstat -gc [insert-pid-here] to find statistics of the behavior of the garbage collected heap.

jstat -gccapacity [insert-pid-here] will present information about memory pool generation and space capabilities.

jstat -gcutil [insert-pid-here] will present the utilization of each generation as a percentage of its capacity. Useful to get an at a glance view of usage.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.