0

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?

3
  • 2
    The way to do this is to use JMX. You can call JMX from Java code, like any other Java code. Commented Jan 7, 2014 at 11:18
  • Or you can do this externally using ps or reading /proc/{pid} if you have Linux. Commented Jan 7, 2014 at 11:19
  • i want cpu usage in percentage by java process. how can i get using JMX? Commented Jan 7, 2014 at 11:59

3 Answers 3

1

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.

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

3 Comments

can you please check how to use getCPUUsage i am using CpuTimes.getCPUUsage but it always returns 1.0
CpuTimes ct = monitor.cpuTimes(); ct.getCpuUsage(ct);
@Saurav you have source code try to debug , we can not tell anything seeing one line. you can start a new question for this problem and provide your code over there I am sure you will get answer.
0

One way would be to use

Runtime.getRuntime().exec(command);

and run top | grep java command.

Comments

0

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 !

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.