Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 477
nadh Avatar asked May 06 '11 06:05

nadh


2 Answers

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.

like image 173
Martin Vysny Avatar answered Oct 17 '22 00:10

Martin Vysny


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.

like image 25
Mike Dunlavey Avatar answered Oct 16 '22 22:10

Mike Dunlavey