Results 1 to 6 of 6
  1. #1
    JavaEmpires is offline Member
    Join Date
    Dec 2007
    Posts
    19
    Rep Power
    0

    Default How to determine CPU usage using Java?????

    Hi all,

    I want to do a CPU monitoring program and for that I need the CPU usage per second.
    How do i determine the CPU usage using Java ???
    Please give any suggestions as to how to go about this???
    OS i am using is Linux.

    Thanks in advance

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    dont worry newbie, we got you covered.

  3. #3
    Deathbob is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default

    There is a way using pure java. It may take some adapting to fit your needs. The monitoring could be placed on it's own thread and have a handle to the thread you wish to monitor.

    cpuperc in this case holds the aprox. cpu usage %. It is fairly accurate. You could average several to get a better idea.

    Java Code:
    //import java.lang.management.*;
    
    ThreadMXBean TMB = ManagementFactory.getThreadMXBean();
    long time = new Date().getTime() * 1000000;
    long cput = 0;
    double cpuperc = -1;
    
    //Begin loop.
    
    if( TMB.isThreadCpuTimeSupported() )
    {
        	if(new Date().getTime() * 1000000 - time > 1000000000) //Reset once per second
        	{
        		time = new Date().getTime() * 1000000;
        		cput = TMB.getCurrentThreadCpuTime();
        	}
        				
        	if(!TMB.isThreadCpuTimeEnabled())
        	{
        		TMB.setThreadCpuTimeEnabled(true);
        	}
        				
        	if(new Date().getTime() * 1000000 - time != 0)
        		cpuperc = (TMB.getCurrentThreadCpuTime() - cput) / (new Date().getTime() * 1000000.0 - time) * 100.0;  				
    }
    else
    {
        cpuperc = -2;
    }
    
    //Do cpu intensive stuff
    
    //End Loop

  4. #4
    jezhumble is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default This project does it

    search for javasysmon on github. It's BSD licensed. Support for Windows, Mac OS X and Linux (so far)

  5. #5
    umamageswari is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default doubt

    hi... i m also working in java... i wanna retrieve the same cpu usage(for system not for the jvm running) using java code... i cannot understand the code given above..
    i can execute and get output for the given code. but even if i use loop and execute the loop once for 5 seconds also the function is not resetting the values. i m getting the same output for how many times i m running the loop...

    also i could not get what does the cpuperc value is??? like i want the result in percentage... and in what unit the result comes??? how can i check whether the value i m getting is correct??? pls give any suggestions...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Best to ask your new question in its own thread -- you'll stand a much better chance of getting a viable answer this way. So I'm going to lock this thread but invite you to post your own question with as much detail as is necessary to answer your question. Feel free to link to this thread.

Similar Threads

  1. Timer usage in java program
    By sandeeprao.techno in forum New To Java
    Replies: 5
    Last Post: 01-24-2008, 07:16 PM
  2. Integer Usage
    By bluegreen7hi in forum New To Java
    Replies: 11
    Last Post: 01-24-2008, 08:54 AM
  3. Replies: 3
    Last Post: 01-19-2008, 06:05 AM
  4. how to determine the primary key
    By osval in forum JDBC
    Replies: 1
    Last Post: 08-07-2007, 02:31 AM
  5. Replies: 1
    Last Post: 05-20-2007, 11:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •