Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

runtime.getRuntime().exec() to slow and to CPU intensive

843798Aug 7 2008 — edited Aug 7 2008
Here is my issue:
I am developing a web page as a server monitoring utility, and part of this project involves reporting the CPU usage for a UNIX (AIX, WebSphere 6) system.
My code works; however, the process I am using causes the CPU to go up 10% (every call) and take 500 milliseconds to perform.

My Code:
-->Using AJAX on a basic HTML page to call java step.(I would like to see this refresh every second and not see it hurt my servers performance)
Java Step:
try
{
Runtime rt = Runtime.getRuntime();

// Had to keep biting nails to figure this out this Unix command

String sVMCmd = "vmstat | tail -1 | tr -s '[:space:]' '[#*]' | cut -d '#' -f 15,16";
String[] cmds = {"/bin/sh", "-c", sVMCmd};

// Execute vmstat with # to filter out data we need

Process p = rt.exec( cmds );
java.io.InputStream in = p.getInputStream();
byte[] b1 = new byte[5];
in.read(b1);

String sFull = new String(b1);
String words[] = sFull.split("#");
us = words[0].toString();
sy = words[1].toString();
us = us.trim();
sy = sy.trim();

in.close();
}
catch(Exception ie)
{
oLog.error(ie);
us = "0";
sy = "0";
}

The Variables us and sy are set to properties later and then returned in XML for the AJAX.

What I am trying to do is return the "us" and "sy" from the vmstat command, I have tried parsing out what I need in my java and found that doing the parsing in my Unix command is much faster (Which explains my complex UNIX command). When I run the UNIX command on the sever it is quick and doesn't pose a issue as far as CPU usage is concerned.
I have broken apart my code step by step and have already determined that the " Process p = rt.exec( cmds ); " is the "Expensive part".
This is the only way I have found to return the CPU usage, and am looking for something quicker and less "Expensive" or a way to tweak what I already have.
I also use the getRuntime() Memory methods and they only take .003 seconds, something that fast would be great.
Any ideas?????
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 4 2008
Added on Aug 7 2008
8 comments
1,428 views