How do I monitor the computer's CPU, memory, and disk usage in Java?

后端 未结 11 2651
粉色の甜心
粉色の甜心 2020-11-22 13:02

I would like to monitor the following system information in Java:

  • Current CPU usage** (percent)
  • Available memory* (free/total)
  • Available d

11条回答
  •  伪装坚强ぢ
    2020-11-22 13:21

    Make a batch file "Pc.bat" as, typeperf -sc 1 "\mukit\processor(_Total)\%% Processor Time"

    You can use the class MProcess,

    /*
     *Md. Mukit Hasan
     *CSE-JU,35
     **/
    import java.io.*;

    public class MProcessor {

    public MProcessor() { String s; try { Process ps = Runtime.getRuntime().exec("Pc.bat"); BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream())); while((s = br.readLine()) != null) { System.out.println(s); } } catch( Exception ex ) { System.out.println(ex.toString()); } }

    }

    Then after some string manipulation, you get the CPU use. You can use the same process for other tasks.

    --Mukit Hasan

提交回复
热议问题