Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding out processor usage  RSS feed

 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Is there any way in Java to access system information like Processor Use (Load) without loosing the platform independance? I have heard that there is a possibility through JNI. But use of JNI will make the application to be platform specific, isn't it?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, pure Java currently does not support getting CPU usage information. You can get it using JNI or by executing an external program (e.g. ps on Unix, tasklist on Windows XP).

You can still support multiple platforms. However, you need an implementation on each platform you want to support. In the JNI case, it's a matter of building your JNI library on each platform, then configuring your installer (or however you deploy the app) to put the right library in the right place. In the external program case, your Java code needs to look at system properties, to see what OS it's running on, so as to choose the right external program.
 
Timothy Wall
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Yohan Liyanage:
Hello,

Is there any way in Java to access system information like Processor Use (Load) without loosing the platform independance? I have heard that there is a possibility through JNI. But use of JNI will make the application to be platform specific, isn't it?


JNA makes it easy to obtain this sort of information directly from the host OS without using JNI, but you have to provide an implementation for each OS that has a different API. Josh Marinacci did an implementation for OSX so he could have a java-based CPU monitor.
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. (Sorry about the late reply.)
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got to know that a new Java 6 supports following method :

java.lang.management.OperatingSystemMXBean.getSystemLoadAverage()

does this method allows us to get a clear picture of the CPU load? Can someone explain the meaning of the return values from this method?

Thanks.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Yohan Liyanage:
I got to know that a new Java 6 supports following method :

java.lang.management.OperatingSystemMXBean.getSystemLoadAverage()

does this method allows us to get a clear picture of the CPU load? Can someone explain the meaning of the return values from this method?


Interesting. I would like to know what this returns too... too bad I am not using Java 6.

In my case, I had to write two mbeans -- one for windows and one for unix. And for unix, I had to detect whether it was solaris or linux, as there are slight variations to the "ps" command.

Henry
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using the method in my Windows environment and I got a -1 . Later I found out that this method does not work in windows because it is too "expensive" to implement this in windows .

Later in the Sun Bug Database, I found that this method works on Linux and Solaris systems, but not in windows.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!