Java 如何查看应用程序正在使用的当前堆大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2015463/
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 to view the current heap size that an application is using?
提问by mrblah
I think I increased my heap size to 1 GB in NetBeans since I changed the config to look like this:
我想我在 NetBeans 中将堆大小增加到 1 GB,因为我将配置更改为如下所示:
netbeans_default_options="-J-Xmx1g ......
After I restarted NetBeans, can I be sure that my app is given 1 GB now?
重新启动 NetBeans 后,我可以确定我的应用程序现在获得了 1 GB 的空间吗?
Is there a way to verify this?
有没有办法验证这一点?
采纳答案by Drewen
Use this code:
使用此代码:
// Get current size of heap in bytes
long heapSize = Runtime.getRuntime().totalMemory();
// Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();
// Get amount of free memory within the heap in bytes. This size will increase // after garbage collection and decrease as new objects are created.
long heapFreeSize = Runtime.getRuntime().freeMemory();
It was useful to me to know it.
知道它对我很有用。
回答by Alexander Torstling
You can use jconsole (standard with most JDKs) to check heap sizes of any java process.
您可以使用 jconsole(大多数 JDK 的标准)来检查任何 Java 进程的堆大小。
回答by Thorbj?rn Ravn Andersen
Attach with jvisualvm from Sun Java 6 JDK. Startup flags are listed.
使用来自 Sun Java 6 JDK 的 jvisualvm 附加。列出了启动标志。
回答by Sujith PS
You can Use the tool : Eclipse Memory Analyzer Tool http://www.eclipse.org/mat/.
您可以使用该工具:Eclipse Memory Analyzer Tool http://www.eclipse.org/mat/。
It is very useful.
这是非常有用的。
回答by ram kumar
public class CheckHeapSize {
public static void main(String[] args) {
// TODO Auto-generated method stub
long heapSize = Runtime.getRuntime().totalMemory();
// Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();
// Get amount of free memory within the heap in bytes. This size will increase // after garbage collection and decrease as new objects are created.
long heapFreeSize = Runtime.getRuntime().freeMemory();
System.out.println("heapsize"+formatSize(heapSize));
System.out.println("heapmaxsize"+formatSize(heapMaxSize));
System.out.println("heapFreesize"+formatSize(heapFreeSize));
}
public static String formatSize(long v) {
if (v < 1024) return v + " B";
int z = (63 - Long.numberOfLeadingZeros(v)) / 10;
return String.format("%.1f %sB", (double)v / (1L << (z*10)), " KMGTPE".charAt(z));
}
}
回答by Matt Fortier
Personal favourite for when jvisualvm is overkill or you need cli-only: jvmtop
当 jvisualvm 过大或只需要 cli 时,个人最喜欢的:jvmtop
JvmTop 0.8.0 alpha amd64 8 cpus, Linux 2.6.32-27, load avg 0.12
https://github.com/patric-r/jvmtop
PID MAIN-CLASS HPCUR HPMAX NHCUR NHMAX CPU GC VM USERNAME #T DL
3370 rapperSimpleApp 165m 455m 109m 176m 0.12% 0.00% S6U37 web 21
11272 ver.resin.Resin [ERROR: Could not attach to VM]
27338 WatchdogManager 11m 28m 23m 130m 0.00% 0.00% S6U37 web 31
19187 m.jvmtop.JvmTop 20m 3544m 13m 130m 0.93% 0.47% S6U37 web 20
16733 artup.Bootstrap 159m 455m 166m 304m 0.12% 0.00% S6U37 web 46