如何查看应用程序正在使用的当前堆大小?
所以我认为我在netbeans中将堆大小增加到1 GB,我将configuration更改为:
netbeans_default_options="-J-Xmx1g ......
我closures并再次启动netbeans,我可以确定我的应用程序现在是1GB? 还是有办法validation这个?
使用此代码:
// 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进程的堆大小。
附上来自Sun Java 6 JDK的jvisualvm。 列出了启动标志。
你可以使用这个工具:Eclipse Memory Analyzer Tool http://www.eclipse.org/mat/ 。
这是非常有用的。
为了检查你的Java应用程序的堆大小和CPU时间,使用JProfiler工具将访问你的JVM并显示所有的堆和CPU时间分配
插件可用于eclipse和intellej Idea IDE
链接下载Jprofiler
公共类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是过度杀伤或你需要唯一的: 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