如何在 Java 中检查 CPU 和内存使用情况?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/74674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check CPU and Memory Usage in Java?
提问by Johnny Bou
I need to check CPU and memory usage for the server in java, anyone know how it could be done?
我需要用java检查服务器的CPU和内存使用情况,有人知道怎么做吗?
回答by Tim Howland
回答by moonshadow
回答by Rich Adams
For memory usage, the following will work,
对于内存使用,以下将起作用,
long total = Runtime.getRuntime().totalMemory();
long used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
For CPU usage, you'll need to use an external application to measure it.
对于 CPU 使用率,您需要使用外部应用程序来衡量它。
回答by blahspam
JConsoleis an easy way to monitor a running Java application or you can use a Profiler to get more detailed information on your application. I like using the NetBeans Profilerfor this.
JConsole是一种监视正在运行的 Java 应用程序的简单方法,您也可以使用 Profiler 获取有关应用程序的更多详细信息。我喜欢为此使用NetBeans Profiler。
回答by Bill K
If you use the runtime/totalMemory solution that has been posted in many answers here (I've done that a lot), be sure to force two garbage collections first if you want fairly accurate/consistent results.
如果您使用在这里的许多答案中发布的 runtime/totalMemory 解决方案(我已经做了很多),如果您想要相当准确/一致的结果,请务必先强制执行两次垃圾回收。
For effiency Java usually allows garbage to fill up all of memory before forcing a GC, and even then it's not usually a complete GC, so your results for runtime.freeMemory() always be somewhere between the "real" amount of free memory and 0.
为了提高效率,Java 通常允许垃圾在强制 GC 之前填满所有内存,即使这样它通常也不是完整的 GC,因此 runtime.freeMemory() 的结果始终介于“真实”可用内存量和 0 之间.
The first GC doesn't get everything, it gets most of it.
第一次 GC 并没有得到一切,它得到了大部分。
The upswing is that if you just do the freeMemory() call you will get a number that is absolutely useless and varies widely, but if do 2 gc's first it is a very reliable gauge. It also makes the routine MUCH slower (seconds, possibly).
上升趋势是,如果您只执行 freeMemory() 调用,您将获得一个绝对无用且变化很大的数字,但如果先执行 2 gc,则这是一个非常可靠的衡量标准。它还使例程慢得多(可能是几秒钟)。
回答by Jeremy
If you are looking specifically for memory in JVM:
如果您正在专门寻找 JVM 中的内存:
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/>");
However, these should be taken only as an estimate...
然而,这些只能作为估计......
回答by Telcontar
Since Java 1.5 the JDK comes with a new tool: JConsolewich can show you the CPU and memory usage of any 1.5 or later JVM. It can do charts of these parameters, export to CSV, show the number of classes loaded, the number of instances, deadlocks, threads etc...
从 Java 1.5 开始,JDK 附带了一个新工具:JConsole,它可以显示任何 1.5 或更高版本 JVM 的 CPU 和内存使用情况。它可以做这些参数的图表,导出到 CSV,显示加载的类数,实例数,死锁,线程等......
回答by Gregg
The YourKitJava profiler is an excellent commercial solution. You can find further information in the docs on CPU profilingand memory profiling.
回答by Javamann
JMX, The MXBeans (ThreadMXBean, etc) provided will give you Memory and CPU usages.
JMX,提供的 MXBeans(ThreadMXBean 等)将为您提供内存和 CPU 使用率。
OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
operatingSystemMXBean.getSystemCpuLoad();
回答by Javamann
If you are using the Sun JVM, and are interested in the internal memory usage of the application (how much out of the allocated memory your app is using) I prefer to turn on the JVMs built-in garbage collection logging. You simply add -verbose:gc to the startup command.
如果您使用的是 Sun JVM,并且对应用程序的内部内存使用情况(您的应用程序使用了多少已分配的内存)感兴趣,我更愿意打开 JVM 内置垃圾收集日志记录。您只需将 -verbose:gc 添加到启动命令。
From the Sun documentation:
从 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:
[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
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
(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.
0.2300771 secs (in the first line)
命令行参数 -verbose:gc 打印每个集合的信息。请注意,-verbose:gc 输出的格式可能会在 J2SE 平台的发行版之间发生变化。例如,这是一个大型服务器应用程序的输出:
[GC 325407K->83000K(776768K), 0.2300771 secs] [GC 325816K->83372K(776768K), 0.2454258 secs] [Full GC 267628K->83769K(776768K), 1.8479984 secs]
在这里,我们看到两个小集合和一个主要集合。箭头前后的数字
325407K->83000K (in the first line)
分别表示垃圾回收前后活动对象的组合大小。在次要集合之后,计数包括不一定处于活动状态但无法回收的对象,或者因为它们直接处于活动状态,或者因为它们在老年代内或从老年代引用。括号内的数字
(776768K) (in the first line)
是总可用空间,不包括永久代中的空间,它是总堆减去一个幸存者空间。次要集合花费了大约四分之一秒。
0.2300771 secs (in the first line)
For more info see: http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html
有关更多信息,请参阅:http: //java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html