• 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

CPU utilization in java

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I want to show CPU utilization in my application using java code.
The code should be OS-independant. Any workarounds are also welcome.
I don't want to use JNI.

can anybody help me in doing that.


Regards

Amit
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Is there any specific reason for not using JNI, because as far as my knowledge goes, you need to do the platform dependent stuff using JNI.
Anyways try this link


Hope this helps
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get CPU info on Linux out of the proc filesystem with standard Java IO classes.
I don't know if you can get the same information on Windows without JNI. Maybe you can find something to run with Process.exec() and parse the output.
There's no way to get around using platform-specific code, in any case.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[AG]: because as far as my knowledge goes, you need to do the platform dependent stuff using JNI.

Well, you may also be able to do this using Runtime.exec() or with a ProcessBuilder. But it's still going to be OS-specific. There's really no way to do this in a platform-neutral way, as far as I know.
[ April 15, 2008: Message edited by: Jim Yingst ]
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Well, you may also be able to do this using Runtime.exec() or with a ProcessBuilder. But it's still going to be OS-specific. There's really no way to do this in a platform-neutral way, as far as I know.


Jim thanks for the reply, I know about Runtime.exec() but I dont have any idea about ProcessBuilder.
Also this might help.

But when I had done similar platform specific tasks, I got desired results with JNI easily,maybe I am wrong.

Anyways thanks again for the reply
[ April 15, 2008: Message edited by: Amit Ghorpade ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JNI may well be the best way to solve this particular problem; I haven't really looked into the details. I was mostly commenting on your general statement "you need to do the platform dependent stuff using JNI". In general platform-dependent stuff can be done with either JNI or exec(). Use JNI if you know how to do it from a C program, and use exec() if you know how to do it from the command line. There may be other ways as well, but those two are the most common approaches.

As for ProcessBuilder, it's basically a nicer version of exec() that became available in JDK 5. You can find the details here: ProcessBuilder
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim, thanks for the reply and the link,
just learned something new because of you

Thank you again
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to find out CPU utilization of a running java application, you can use jmx. You should look at OperatingSystemMBean
http://java.sun.com/javase/6/docs/jre/api/management/extension/com/sun/management/OperatingSystemMXBean.html
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, that's good to know. Thanks.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic