| Author |
CPU utilization in java
|
amit pandit
Greenhorn
Joined: Mar 01, 2007
Posts: 23
|
|
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
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2561
|
|
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
|
SCJP, SCWCD.
|Asking Good Questions|
|
 |
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8290
|
|
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.
|
"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
[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 ]
|
"I'm not back." - Bill Harding, Twister
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2561
|
|
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
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
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
Joined: Jun 06, 2007
Posts: 2561
|
|
Hi Jim, thanks for the reply and the link, just learned something new because of you Thank you again
|
 |
Ajay Singh
Ranch Hand
Joined: Dec 13, 2006
Posts: 182
|
|
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
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
|
Ah, that's good to know. Thanks.
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2561
|
|
Yes Thanks
|
 |
 |
|
|
subject: CPU utilization in java
|
|
|