如何看待当前的堆大小,一个应用程序使用?
-
19-09-2019 - |
题
我想我增加了我的堆大小为1 GB在NetBeans,因为我改变了配置看起来是这样的:
netbeans_default_options="-J-Xmx1g ......
在我重新启动的NetBeans,我能确保我的应用程序提供1个GB现在?
有没有办法来验证这一点?
解决方案
使用此代码:
// 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();
这是对我有用的知道这一点。
其他提示
可以使用JConsole的(与大多数的JDK标准)来检查任何java程序的堆的大小。
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));
}
}
与jvisualvm来自Sun Java 6的JDK附加。启动标志被列出。
您可以使用该工具:Eclipse的内存分析工具 http://www.eclipse.org/mat/
这是非常有用。
个人最喜欢的时候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
有关检查将要访问的JVM并显示所有堆和CPU时间分配Java应用程序堆大小和CPU时间的JProfiler使用工具
塞在可用于蚀和intellej思想IDE
链接,下载的JProfiler
不隶属于 StackOverflow