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. Advertisements

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. mk
    Replies:
    1
    Views:
    10,627
    Robert Olofsson
    Jul 25, 2003
  2. Wenbo
    Replies:
    0
    Views:
    529
    Wenbo
    Feb 17, 2004
  3. blackdog

    Java vs. CPU and memory

    blackdog, Jan 25, 2006, in forum: Java
    Replies:
    7
    Views:
    1,008
    Oliver Wong
    Jan 25, 2006
  4. hvt
    Replies:
    0
    Views:
    1,782
  5. anonym0us
    Replies:
    1
    Views:
    462
    Mark Space
    Oct 23, 2008
  6. pavunkumar

    How , system cpu and user cpu times calculates

    pavunkumar, Feb 27, 2009, in forum: C Programming
    Replies:
    1
    Views:
    528
  7. Krist
    Replies:
    8
    Views:
    9,928
    Arne Vajhøj
    Feb 10, 2010
  8. Luki
    Replies:
    1
    Views:
    229
Loading...