Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Geting the CPU usage of a machine

 
Sultan Al-Yahyai
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I want to find the CPU usage of a machine in the network or the localhost. is that possible in Java? if so please help, otherwise if any one know a utility to do so please tell me. things also such as available memory / hard disk ... are intersting to me as well
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't a built in way to get the CPU usage in Java... you'd have to write code in C (or some other language) and use JNI to call the C program, or use Runtime.exec() to call an existing program that gives you this information. Here's a JavaWorld article about this.

There is also no built in way to get the available free space on the hard drive... you're once again stuck calling a system command through Runtime.exec(), using JNI to a program in another language, or using this ugly workaround code posted in Sun's forums.

And again there is no built in way to get the total/free RAM memory in the system either... you can call Runtime.totalMemory() and Runtime.freeMemory() but they just report on memory available inside the JVM. So once again, you are stuck writing something in native code, or using Runtime.exec() to call a command line utility.

The reason that none of these things are available in Java is because Java runs in a virtual machine that is independent of hardware, and all of these functions are a little "too close to the metal"...

As far as how to distribute them to work on "any machine in the network, or the localhost", you'd need to implement a little RMI server that would do these functions and run it on each machine. Then you could write a client application that would query a specified machine to return the data that was asked for.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool - just this morning a reference to this article on getting at Windows Performance data appeared in my inbox.
If you try it, please let us know how it works for you.
Bill
 
kk zhu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can use NSClient package to get cpu usage on win32 platform, but it can't support Solaris OS.
Does anyone extend it to support unix/linux OS?

Originally posted by William Brogden:
Cool - just this morning a reference to this article on getting at Windows Performance data appeared in my inbox.
If you try it, please let us know how it works for you.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic