我需要在Java中检查服务器的CPU和内存使用情况,有人知道怎么做吗?
如果您正在专门寻找JVM中的内存:
1 2 3 4 5 6 7 8 9 10 11 12 13
| Runtime runtime = Runtime.getRuntime();
NumberFormat format = NumberFormat.getInstance();
StringBuilder sb = new StringBuilder();
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
sb.append("free memory:" + format.format(freeMemory / 1024) +"<br/>");
sb.append("allocated memory:" + format.format(allocatedMemory / 1024) +"<br/>");
sb.append("max memory:" + format.format(maxMemory / 1024) +"<br/>");
sb.append("total free memory:" + format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024) +"<br/>"); |
但是,这些仅应作为估计...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| package mkd.Utils;
import java.io.File;
import java.text.NumberFormat;
public class systemInfo {
private Runtime runtime = Runtime.getRuntime();
public String Info() {
StringBuilder sb = new StringBuilder();
sb.append(this.OsInfo());
sb.append(this.MemInfo());
sb.append(this.DiskInfo());
return sb.toString();
}
public String OSname() {
return System.getProperty("os.name");
}
public String OSversion() {
return System.getProperty("os.version");
}
public String OsArch() {
return System.getProperty("os.arch");
}
public long totalMem() {
return Runtime.getRuntime().totalMemory();
}
public long usedMem() {
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}
public String MemInfo() {
NumberFormat format = NumberFormat.getInstance();
StringBuilder sb = new StringBuilder();
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
sb.append("Free memory:");
sb.append(format.format(freeMemory / 1024));
sb.append("<br/>");
sb.append("Allocated memory:");
sb.append(format.format(allocatedMemory / 1024));
sb.append("<br/>");
sb.append("Max memory:");
sb.append(format.format(maxMemory / 1024));
sb.append("<br/>");
sb.append("Total free memory:");
sb.append(format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024));
sb.append("<br/>");
return sb.toString();
}
public String OsInfo() {
StringBuilder sb = new StringBuilder();
sb.append("OS:");
sb.append(this.OSname());
sb.append("<br/>");
sb.append("Version:");
sb.append(this.OSversion());
sb.append("<br/>");
sb.append(":");
sb.append(this.OsArch());
sb.append("<br/>");
sb.append("Available processors (cores):");
sb.append(runtime.availableProcessors());
sb.append("<br/>");
return sb.toString();
}
public String DiskInfo() {
/* Get a list of all filesystem roots on this system */
File[] roots = File.listRoots();
StringBuilder sb = new StringBuilder();
/* For each filesystem root, print some info */
for (File root : roots) {
sb.append("File system root:");
sb.append(root.getAbsolutePath());
sb.append("<br/>");
sb.append("Total space (bytes):");
sb.append(root.getTotalSpace());
sb.append("<br/>");
sb.append("Free space (bytes):");
sb.append(root.getFreeSpace());
sb.append("<br/>");
sb.append("Usable space (bytes):");
sb.append(root.getUsableSpace());
sb.append("<br/>");
}
return sb.toString();
}
} |
如果您使用的是Sun JVM,并且对应用程序的内部内存使用情况(应用程序使用的已分配内存中的多少)感兴趣,那么我更喜欢打开JVM的内置垃圾收集日志记录。您只需在启动命令中添加-verbose:gc即可。
从Sun文档:
The command line argument -verbose:gc prints information at every
collection. Note that the format of the -verbose:gc output is subject
to change between releases of the J2SE platform. For example, here is
output from a large server application:
1 2 3
| [GC 325407K->83000K(776768K), 0.2300771 secs]
[GC 325816K->83372K(776768K), 0.2454258 secs]
[Full GC 267628K->83769K(776768K), 1.8479984 secs] |
Here we see two minor collections and one major one. The numbers
before and after the arrow
1
| 325407K->83000K (in the first line) |
indicate the combined size of live objects before and after garbage
collection, respectively. After minor collections the count includes
objects that aren't necessarily alive but can't be reclaimed, either
because they are directly alive, or because they are within or
referenced from the tenured generation. The number in parenthesis
1
| (776768K) (in the first line) |
is the total available space, not counting the space in the permanent
generation, which is the total heap minus one of the survivor spaces.
The minor collection took about a quarter of a second.
1
| 0.2300771 secs (in the first line) |
有关更多信息,请参见:http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html
从这里
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
int availableProcessors = operatingSystemMXBean.getAvailableProcessors();
long prevUpTime = runtimeMXBean.getUptime();
long prevProcessCpuTime = operatingSystemMXBean.getProcessCpuTime();
double cpuUsage;
try
{
Thread.sleep(500);
}
catch (Exception ignored) { }
operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
long upTime = runtimeMXBean.getUptime();
long processCpuTime = operatingSystemMXBean.getProcessCpuTime();
long elapsedCpu = processCpuTime - prevProcessCpuTime;
long elapsedTime = upTime - prevUpTime;
cpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 10000F * availableProcessors));
System.out.println("Java CPU:" + cpuUsage); |
JMX,提供的MXBean(ThreadMXBean等)将为您提供内存和CPU使用率。
1 2
| OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
operatingSystemMXBean.getSystemCpuLoad(); |
对于内存使用,以下方法将起作用,
1 2
| long total = Runtime.getRuntime().totalMemory();
long used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); |
对于CPU使用率,您需要使用外部应用程序进行测量。
从Java 1.5开始,JDK附带了一个新工具:JConsole wich可以向您显示任何1.5或更高版本JVM的CPU和内存使用情况。它可以绘制这些参数的图表,导出为CSV,显示加载的类数,实例数,死锁,线程等。
如果您使用此处许多答案中都已发布的运行时/ totalMemory解决方案(我已经做了很多),如果您想要相当准确/一致的结果,请确保首先强制执行两个垃圾回收。
为了提高效率,Java通常允许垃圾在强制GC之前填满所有内存,即使那样,它通常也不是完整的GC,因此,runtime.freeMemory()的结果始终在"实际"可用内存量和0之间。 。
第一个GC不能得到所有,它可以得到大部分。
向上发展的是,如果您仅执行freeMemory()调用,您将获得一个绝对没有用的数字,并且变化很大,但是如果首先执行2 gc,则它将是一个非常可靠的指标。这也使例行MUCH变慢(可能是几秒钟)。
Java的Runtime对象可以报告JVM的内存使用情况。对于CPU消耗,您必须使用外部实用程序,例如Unix的top或Windows Process Manager。
我还将添加以下跟踪CPU负载的方法:
1 2 3 4 5 6 7 8 9
| import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
double getCpuLoad() {
OperatingSystemMXBean osBean =
(com.sun.management.OperatingSystemMXBean) ManagementFactory.
getPlatformMXBeans(OperatingSystemMXBean.class);
return osBean.getProcessCpuLoad();
} |
你可以在这里阅读更多
这是一些简单的代码,用于计算当前的内存使用量(以兆字节为单位):
1
| double currentMemory = ( (double)((double)(Runtime.getRuntime().totalMemory()/1024)/1024))- ((double)((double)(Runtime.getRuntime().freeMemory()/1024)/1024)); |
YourKit Java分析器是一种出色的商业解决方案。您可以在有关CPU性能分析和内存性能分析的文档中找到更多信息。
JConsole是监视正在运行的Java应用程序的简便方法,或者您可以使用Profiler来获取有关应用程序的更多详细信息。我喜欢为此使用NetBeans Profiler。
如果您使用的是Tomcat,请查看Psi探针,该探针可用于监视内部和外部内存消耗以及其他许多区域。
对于Eclipse,您可以使用TPTP(测试和性能工具平台)来分析内存使用情况等。更多信息