retrieving CPU Usage and Memory Usage information in JAVA

Discussion in 'Java' started by hvt, Mar 13, 2007.

  1. hvt

    hvt Guest

    Hi All,
    I 've been searching through this group for information on CPU and
    Memory usage retrieval on Windows machine using JAVA. After following
    so many previous threads on the same subject, the possible solution I
    've observed was using JNI calls to native libraries for these OS
    specific information retrieval.
    Later I found that java management libraries already has it, native
    libraries and some jmx classes.

    Sharing the code, hope this may help group members in their
    requirements.

    Code:
    com.sun.management.OperatingSystemMXBean mxbean =

    (com.sun.management.OperatingSystemMXBean)
    ManagementFactory.getOperatingSystemMXBean();

    System.out.println("********* CPU & MEMORY USAGE OF \\harsh
    *********");
    System.out.println("OS : "+mxbean.getName());
    System.out.println("Available Processors :
    "+mxbean.getAvailableProcessors());
    System.out.println("Commited Virtual Memory :
    "+mxbean.getCommittedVirtualMemorySize());
    System.out.println("Free Physical Memory :
    "+mxbean.getFreePhysicalMemorySize());
    System.out.println("Free Swap Space Size :
    "+mxbean.getFreeSwapSpaceSize());
    System.out.println("Process CPU Time : "+mxbean.getProcessCpuTime());
    System.out.println("Total Physical Memory Size :
    "+mxbean.getTotalPhysicalMemorySize());
    System.out.println("Total Swap Space Size :
    "+mxbean.getTotalSwapSpaceSize());

    Problem:
    getProcessCpuTime() method returns the CPU time used by the process on
    which the Java virtual machine is running NOT the '%CPU Usage' time
    which i was hoping to get.

    If anyone is having idea about how to obtain '%CPU Usage' of windows
    machine using java program, kindly post the message.

    Regards,
    hvt, Mar 13, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wenbo
    Replies:
    0
    Views:
    385
    Wenbo
    Feb 17, 2004
  2. hvt
    Replies:
    0
    Views:
    1,469
  3. pavunkumar

    How , system cpu and user cpu times calculates

    pavunkumar, Feb 27, 2009, in forum: C Programming
    Replies:
    1
    Views:
    350
  4. Kant
    Replies:
    3
    Views:
    88
    ZeBest Kant
    Jan 19, 2004
  5. Luki
    Replies:
    1
    Views:
    78
Loading...

Share This Page