Saurav
Saurav

Reputation: 248

how to find cpu usage by java process

I want to moniter cpu usage of some java application. I don't want to use tools like jconsole (JMX)

if there is any way to check by java code?

Upvotes: 0

Views: 8872

Answers (3)

Pierre Laporte
Pierre Laporte

Reputation: 1215

You should retrieve the MX Bean ThreadMXBean from the Java class itself and use the methods get*CpuTime().

Otherwise, you can use good old top and grep on your Java process PID.

If you are brave enough, there is also perf tool in linux-tools package.

Hope that helps !

Upvotes: 0

Vipin
Vipin

Reputation: 5223

Use JavaSysMon

Currently it reports:

  • Process table (also available as a tree), including pid, ppid, name, command line, user, process size, resident process size, user time, and kernel time.
  • Uptime
  • Current process’s pid
  • Total and free RAM
  • Total and free swap
  • CPU usage
  • CPU frequency
  • Number of CPUs

Also you can use jmx mbeans or get PID using Runtime.exec() and using that call platform-specific command like ps.

Upvotes: 1

Aniket Thakur
Aniket Thakur

Reputation: 68915

One way would be to use

Runtime.getRuntime().exec(command);

and run top | grep java command.

Upvotes: 0

Related Questions