2013-02-26 64 views
0

如何找到可用的系統內存,磁盤空間,使用Windows操作系統的CPU使用從Java的系統調用?如何找到可用的系統內存,磁盤空間,CPU使用率從使用系統調用從Java到Windows操作系統

*在windows操作系統

笏我真正想要做的是,,,接受來自客戶端的用戶文件,並在服務器上的文件保存檢查可用空間之後。我的服務器和客戶端使用java tcp socket程序連接!

+2

可能重複(http://stackoverflow.com/questions/25552/using-java-to-get-os-level-system-information) – John3136 2013-02-26 06:08:17

回答

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()); 
    } 
    } 
} 
[使用Java來獲得操作系統級的系統信息]的
+0

我想知道整個系統的所有這些細節,即。每個磁盤可用的內存,cpu資源被遺漏以供使用,但不涉及JVM – user2109489 2013-02-26 06:46:54