Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CPU utilization in java

 
amit pandit
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
 
Joe Ess
Bartender
Posts: 9320
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim, thanks for the reply and the link,
just learned something new because of you

Thank you again
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that's good to know. Thanks.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic