[Java] CPU, Memory 사용량

메소드

  • getSystemCpuLoad() : Returns the "recent cpu usage" for the whole system

  • getTotalPhysicalMemorySize() :Returns the total amount of physical memory in bytes

  • getFreePhysicalMemorySize() : Returns the amount of free physical memory in bytes.

테스트 코드 : CPU사용률, 메모리 잔여량, 전체 물리메모리량

package org.kyhslam.doit;

import com.sun.management.OperatingSystemMXBean;

import java.lang.management.ManagementFactory;

public class Monitoring {
    public static void main(String[] args) {
        try {

            OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);

            for(int i=0; i < 100; i++){
                System.out.println("===================");
                System.out.println("CPU Usage : " + String.format("%.2f", osBean.getSystemCpuLoad() * 100 ));
                System.out.println("Memory Free Space : " + String.format("%.2f", (double)osBean.getFreePhysicalMemorySize()/1024/1024/1024));
                System.out.println("Memory Total Space : " + String.format("%.2f", (double)osBean.getTotalPhysicalMemorySize()/1024/1024/1024  ));

                Thread.sleep(1000);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

결과

'📕 Programing > Java' 카테고리의 다른 글

[Java] 람다식(Lambda Expression)  (0) 2020.12.01
[Java] 람다식(Lambda Expression) 실습  (0) 2020.07.28
JVM(Java Virtual Machine) 내부 구조  (0) 2020.05.29
[Java] 스택(Stack)과 큐(Queue)  (0) 2020.05.22
배열의 선언  (0) 2020.05.13

Designed by JB FACTORY