How to get Ram size and size of Hard disk using Java?

前端 未结 4 801
闹比i
闹比i 2020-12-28 16:44

How to get the Ram size and Hard disk size of the PC using Java? And Is it possible to get the currently logged user name on PC through java?

4条回答
  •  不知归路
    2020-12-28 17:03

    Disk size:

    long diskSize = new File("/").getTotalSpace();
    

    User name:

    String userName = System.getProperty("user.name");
    

    I'm not aware of a reliable way to determine total system memory in Java. On a Unix system you could parse /proc/meminfo. You can of course find the maximum memory available to the JVM:

    long maxMemory = Runtime.getRuntime().maxMemory();
    

    Edit: for completeness (thanks Suresh S), here's a way to get total memory with the Oracle JVM only:

    long memorySize = ((com.sun.management.OperatingSystemMXBean) ManagementFactory
            .getOperatingSystemMXBean()).getTotalPhysicalMemorySize();
    

提交回复
热议问题