如何使用从java到Windows OS的系统调用查找可用的系统内存,磁盘空间,CPU使用情况

时间:2013-02-26 06:05:06

标签: java

如何使用java的系统调用查找可用的系统内存,磁盘空间,Windows操作系统的CPU使用情况?

Windows操作系统中的

*

实际上我想要做的是,,,接受来自客户端用户的文件,并在检查可用空间后将文件保存在服务器中。我的服务器和客户端使用java tcp套接字程序连接!

1 个答案:

答案 0 :(得分:1)

您可以使用以下程序获取一些有限的信息。这已经被同一论坛中的其他人回答了,我只是在复制它。

import java.io.File;

public class MemoryInfo {
  public static void main(String[] args) {
    /* Total number of processors or cores available to the JVM */
    System.out.println("Available processors (cores): " + 
        Runtime.getRuntime().availableProcessors());

    /* Total amount of free memory available to the JVM */
    System.out.println("Free memory (bytes): " + 
        Runtime.getRuntime().freeMemory());

    /* This will return Long.MAX_VALUE if there is no preset limit */
    long maxMemory = Runtime.getRuntime().maxMemory();
    /* Maximum amount of memory the JVM will attempt to use */
    System.out.println("Maximum memory (bytes): " + 
        (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));

    /* Total memory currently in use by the JVM */
    System.out.println("Total memory (bytes): " + 
        Runtime.getRuntime().totalMemory());

    /* Get a list of all filesystem roots on this system */
    File[] roots = File.listRoots();

    /* For each filesystem root, print some info */
    for (File root : roots) {
      System.out.println("File system root: " + root.getAbsolutePath());
      System.out.println("Total space (bytes): " + root.getTotalSpace());
      System.out.println("Free space (bytes): " + root.getFreeSpace());
      System.out.println("Usable space (bytes): " + root.getUsableSpace());
    }
  }
}