问题
我目前正在构建一个Java应用程序,最终可能会在许多不同的平台上运行,但主要是Solaris,Linux和Windows的变种。
有没有人能够成功提取信息,例如当前使用的磁盘空间,CPU利用率和底层操作系统中使用的内存? Java应用程序本身正在消耗什么呢?
我更希望在不使用JNI的情况下获取此信息。
#1 热门回答(192 赞)
你可以从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());
}
}
}
#2 热门回答(91 赞)
java.lang.managementpackage确实为你提供了比运行时更多的信息 - 例如,它将为你提供与非堆内存(ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage()
)分开的堆内存(ManagementFactory.getMemoryMXBean().getHeapMemoryUsage()
)。
你还可以获得进程CPU使用率(无需编写自己的JNI代码),但需要将java.lang.management.OperatingSystemMXBean
转换为acom.sun.management.OperatingSystemMXBean
。这适用于Windows和Linux,我没有在其他地方测试过。
例如...更频繁地调用get getCpuUsage()方法以获得更准确的读数。
public class PerformanceMonitor {
private int availableProcessors = getOperatingSystemMXBean().getAvailableProcessors();
private long lastSystemTime = 0;
private long lastProcessCpuTime = 0;
public synchronized double getCpuUsage()
{
if ( lastSystemTime == 0 )
{
baselineCounters();
return;
}
long systemTime = System.nanoTime();
long processCpuTime = 0;
if ( getOperatingSystemMXBean() instanceof OperatingSystemMXBean )
{
processCpuTime = ( (OperatingSystemMXBean) getOperatingSystemMXBean() ).getProcessCpuTime();
}
double cpuUsage = (double) ( processCpuTime - lastProcessCpuTime ) / ( systemTime - lastSystemTime );
lastSystemTime = systemTime;
lastProcessCpuTime = processCpuTime;
return cpuUsage / availableProcessors;
}
private void baselineCounters()
{
lastSystemTime = System.nanoTime();
if ( getOperatingSystemMXBean() instanceof OperatingSystemMXBean )
{
lastProcessCpuTime = ( (OperatingSystemMXBean) getOperatingSystemMXBean() ).getProcessCpuTime();
}
}
}
#3 热门回答(40 赞)
我认为最好的方法是实现SIGAR API by Hyperic。它适用于大多数主要操作系统(靠近任何现代设备)并且非常易于使用。开发人员对他们的论坛和邮件列表非常敏感。我也喜欢它是GPL2Apache licensed。他们在Java中也提供了大量的例子!
SIGAR == System Information, Gathering And Reporting tool.