0

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

*在windows操作系统中

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

4

1 回答 1

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());
    }
  }
}
于 2013-02-26T06:12:51.033 回答