java-如何查看应用程序正在使用的当前堆大小?
我想在NetBeans中将堆大小增加到1 GB,因为我将配置更改为如下所示:
netbeans_default_options="-J-Xmx1g ......
重新启动NetBeans之后,可以确定我的应用程序现在有1 GB的存储空间吗?
有没有办法验证这一点?
7个解决方案
158 votes
使用此代码:
// 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();
知道这一点对我很有用。
Drewen answered 2020-01-07T18:43:30Z
8 votes
您可以使用jconsole(大多数JDK的标准配置)来检查任何Java进程的堆大小。
Alexander Torstling answered 2020-01-07T18:43:50Z
6 votes
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));
}
}
ram kumar answered 2020-01-07T18:44:05Z
2 votes
随附Sun Java 6 JDK中的jvisualvm。 列出启动标志。
Thorbjørn Ravn Andersen answered 2020-01-07T18:44:25Z
1 votes
您可以使用该工具:Eclipse内存分析器工具[http://www.eclipse.org/mat/]。
这是非常有用的。
Sujith PS answered 2020-01-07T18:44:49Z
0 votes
当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
Matt Fortier answered 2020-01-07T18:45:09Z
-1 votes
要检查您的Java应用程序堆大小和CPU时间,请使用JProfiler工具,该工具将访问JVM并显示所有堆和CPU时间分配
插件可用于Eclipse和Intellij Idea IDE
下载链接Jprofiler
[http://www.ej-technologies.com/download/jprofiler/files]
rinkesh answered 2020-01-07T18:45:42Z