我目前正在构建一个 Java 应用程序,该应用程序最终可以在许多不同的平台上运行,但主要是 Solaris、Linux 和 Windows 的变体。
有没有人能够成功提取底层操作系统中当前使用的磁盘空间、CPU 利用率和内存等信息? Java 应用程序本身消耗的是什么?
我最好不使用 JNI 来获取此信息。
你可以从Runtime类中获取一些有限的内存信息。它确实不完全是您正在寻找的东西,但我想我会为了完整性而提供它。这是一个小例子。编辑:您还可以从 java.io.File 类获取磁盘使用信息。磁盘空间使用情况需要 Java 1.6 或更高版本。
public class Main {
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 available to the JVM */
System.out.println("Total memory available to JVM (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());
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)