0

I have a java application running on my local machine. I want to get the memory and CPU usage of that java application periodically using a java class. I can see the statistics of the application using JCONSOLE. But I want to get numerical values periodically using a java class. The solution should be applicable to both linux and windows platforms. How can I do this?

Asanka sanjaya
  • 1,461
  • 3
  • 17
  • 35

2 Answers2

0

You might find some useful methods under Runtime for example,

Runtime rt = Runtime.getRuntime();
System.out.println("Available Processors: " + rt.availableProcessors());
System.out.println("Free Memory: " + rt.freeMemory());
System.out.println("Total Memory: " + rt.totalMemory());
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thank you for the answer. Can you tell me how can I get those information for only one application. I can get the process ID of that application. – Asanka sanjaya Jul 28 '14 at 05:39
  • How many applications are you running per JVM? Assuming it's one the above answer should would for you. – Elliott Frisch Jul 28 '14 at 05:43
  • Jconsole shows me there are 6 java applications running in my machine. – Asanka sanjaya Jul 28 '14 at 05:46
  • If you read the [documentation](http://docs.oracle.com/javase/7/docs/technotes/guides/management/jconsole.html), and look at this [figure](http://docs.oracle.com/javase/7/docs/technotes/guides/management/jconsole.html#gdhqe) especially helpful. – Elliott Frisch Jul 28 '14 at 05:53
0

Java VisualVM can be used "to monitor a local application and view real-time, high-level data on the memory heap, thread activity, and the classes loaded in the Java Virtual Machine (JVM). Monitoring an application imposes low overhead and can be used for extended periods."

See also How to monitor Java memory usage? for other possibilities.

Community
  • 1
  • 1
DavidPostill
  • 7,734
  • 9
  • 41
  • 60