• 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

Progamatic Access to CPU and Memory Stats

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all. I need to progamatically access CPU (% used per processor) and memory information (total physical and virtual, total used) from the operating system. I know that in native Java I can only really expect to get the VM memory details. That's simply not good enough for my project and so we are ready to step out of pure Java.

My understanding is that JNI is probably the best approach but we are open to suggestions. I recognize that we will end up having a different solution per platform. While that is disagreeable, I don't seem to have any other choice.

The only good example I have found is the one located at JavaWorld:
http://www.javaworld.com/javaworld/javaqa/2002-11/01-qa-1108-cpu.html

That example only provides code for Windows versions newer then 98. Is anyone familiar with another way to get at this information? Or has someone created code that works with other platforms? At this point, we are more then willing to hire a C/C++ developer to create the code for us.

I'd love to hear any suggestions or advice. Thanks!

Mike Grundvig
mike@electrotank.com
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I question the requirement to build such an application in Java. If the guts are to be all in native C then why Java at all? Java is great but it isn't a suitable tool for building whatever monitoring and administration tool you seem to have in mind.
 
Michael Grundvig
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The application itself is actually a large NIO-based server. We have a real-time monitoring console that can be used remotely over the web. We simply want to expose some system-level information via the console. I'd much rather use pure Java for it, but that might not be an option. Thanks!

Mike Grundvig
mike@electrotank.com
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael - That link is pretty old.

You do not say what version of Java, but I suggest you look at Java Management Extensions.

JMX

If you do use home grown JNI, make sure you (somehow) instrument the cost of the JNI calls themselves.

Regards, Guy
 
Michael Grundvig
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've actually taken a look at JMX and it didn't seem to provide any of the data I needed either. Am I missing it and it's burried in there somewhere? Thanks!

Mike
 
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 Michael Grundvig:
I've actually taken a look at JMX and it didn't seem to provide any of the data I needed either. Am I missing it and it's burried in there somewhere? Thanks!

Mike



JMX is merely a management infrastructure, it enables what you need, but ...

Anyway, the JMX container in the Java 5 JVM has stuff similar, but is designed to report at a JVM level. It will report the memory usage for the JVM -- not the whole machine. It will report the thread usage for the JVM -- not the whole machine.

This is probably true of the Tomcat, Weblogic, Websphere, JBoss, etc. JMX containers. At least, I never came across a particular MBean that reports at that the hardware level -- it was always reporting at the level for the application. (which is why we had to write them ourselves)


Anyway, to answer your question, it is probably best to use Runtime.exec() to the OS, and parse the data that you need. For Unix, you can use "ps", "vmstat", or the "/proc" filesystem. For Windows, you can use "typeperf".

Henry
[ January 29, 2006: Message edited by: Henry Wong ]
 
Michael Grundvig
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd thought about runtime exec but not seriously. Never dawned on me to use typeperf. I think that's the approach I'll take. Thanks!

Mike
 
reply
    Bookmark Topic Watch Topic
  • New Topic