본문 바로가기
개발/Java

[JAVA] CPU , Memory , Disk 사용량 확인하기

by 코딩하는 흰둥이 2024. 7. 9.

OperatingSystemMXBean 를 사용하여 CPU와 Memory를,

File 을 사용하여 Disk 용량을 확인하려 한다

 

TEST
import com.sun.management.OperatingSystemMXBean;

import java.io.File;
import java.lang.management.ManagementFactory;

public class RunTestController {
    public static void main(String [] args) throws Exception {
        try{
            OperatingSystemMXBean mxBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);

            File root = null;

            for (int i = 0; i < 100; i++) {

                root = new File("/");

                System.out.println("====================================");
                System.out.println("CPU : " + String.format("%.2f" , mxBean.getSystemCpuLoad() * 100));
                System.out.println("Memory Free : " + String.format("%.2f", (double)mxBean.getFreePhysicalMemorySize()/1024/1024/1024));
                System.out.println("Memory Total : " + String.format("%.2f" , (double)mxBean.getTotalPhysicalMemorySize()/1024/1024/1024));
                System.out.println("Disk Free : " + Math.round(root.getUsableSpace() / Math.pow(1024 , 3)));
                System.out.println("Disk Total : " + Math.round(root.getTotalSpace() / Math.pow(1024 , 3)));
                System.out.println("====================================");

                // 10초마다 사용량 조회
                Thread.sleep(10000);
            }

        }catch (Exception e){
            System.out.println(e);
        }
    }
}

 

 

10초마다 PC 사용량을 조회한다

댓글