nadh
nadh

Reputation: 1231

measure CPU usage of the JVM : java code

Is there a way to measure CPU usage of the JVM (once a java application is started) cross platform (windows + unix + mac)? I have used Jconsole but what I need is a java code that does this, and not a tool through which I can monitor CPU utilization. I have tried out

ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage()

using JMX, but it doesn't help since what I need is the specific CPU usage by the JVM (say when I start a server), not the system load average.

Upvotes: 14

Views: 40149

Answers (7)

Thomas Jung
Thomas Jung

Reputation: 33092

You should have a look at the ThreadMXBean.getThreadCPUTime() method from the Thread MBean.

Thread CPU time

A Java virtual machine implementation may support measuring the CPU time for the current thread, for any thread, or for no threads.

There is also the JTop sample application that's part of the JDK jdk\demo\management\JTop\src\JTop.java or here. Have a look at:

/**
 * Get the thread list with CPU consumption and the ThreadInfo for each thread
 * sorted by the CPU time.
 */
private List<Map.Entry<Long, ThreadInfo>> getThreadList()

Upvotes: 9

Martin Vysny
Martin Vysny

Reputation: 3201

You can try to use https://github.com/mvysny/webmon . You just add the webmon-analyzer jar to your project and you'll start the sampler and TCP/IP server. The sampler will poll JMX API and will keep the stats for awhile, you can then simply telnet localhost 5455 to get a text dump of those stats. Awesome for production. Disclaimer: I'm the author, and the project is old-ish.

Upvotes: 1

danieln
danieln

Reputation: 4973

From here

    com.sun.management.OperatingSystemMXBean operatingSystemMXBean = 
         (com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    int availableProcessors = operatingSystemMXBean.getAvailableProcessors();
    long prevUpTime = runtimeMXBean.getUptime();
    long prevProcessCpuTime = operatingSystemMXBean.getProcessCpuTime();
    double cpuUsage;
    try 
    {
        Thread.sleep(500);
    } 
    catch (Exception ignored) { }

    operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    long upTime = runtimeMXBean.getUptime();
    long processCpuTime = operatingSystemMXBean.getProcessCpuTime();
    long elapsedCpu = processCpuTime - prevProcessCpuTime;
    long elapsedTime = upTime - prevUpTime;

    cpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 10000F * availableProcessors));
    System.out.println("Java CPU: " + cpuUsage);

Upvotes: 2

Wivani
Wivani

Reputation: 2046

Perhaps this or similar libraries could help you out.

Upvotes: 1

Mike Dunlavey
Mike Dunlavey

Reputation: 40669

At any given instant, a thread is either running (100% of core) or not (0%). There is no in-between. What you need is a short-term series of snapshots of the thread's running state, and average it over those.

Upvotes: 2

Nirmit Shah
Nirmit Shah

Reputation: 758

Java itself not providing this feature. There are couple of opensource APIs available to measure CPU Usage.
I recommend Sigar API. Apart from CPU usage, you can get lot more other features like memory usage, System uptime etc.

Upvotes: 1

Muhammad Imran Tariq
Muhammad Imran Tariq

Reputation: 23352

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.

See this link

Upvotes: 0

Related Questions