• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finding out processor usage

 
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic