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
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());
}}
If I'm not mistaken, this class is included in rt.jar, present in your java runtime.