Come visualizzare la dimensione heap corrente che un'applicazione sta usando?
-
19-09-2019 - |
Domanda
Credo di aver aumentato la mia dimensione di heap a 1 GB in NetBeans da quando ho cambiato il config per assomigliare a questo:
netbeans_default_options="-J-Xmx1g ......
Dopo ho ricominciato NetBeans, posso essere sicuro che la mia applicazione è dato 1 GB ora?
C'è un modo per verificare questo?
Soluzione
Utilizzare questo codice:
// 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();
E 'stato utile per me saperlo.
Altri suggerimenti
È possibile utilizzare jconsole (standard con la maggior parte JDK) per verificare la dimensione heap di qualsiasi processo 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));
}
}
Allega con jvisualvm da Sun Java JDK 6. bandiere di avvio sono elencati.
È possibile utilizzare lo strumento: Eclipse Memory Analyzer Tool http://www.eclipse.org/mat/ .
E 'molto utile.
preferito per quando jvisualvm è eccessivo o avete bisogno di cli-only: 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
Per controllare i vostri dimensione heap e tempo CPU strumenti uso JProfiler applicazione Java che accederanno JVM e potrai vedere tutti mucchio e CPU allocazione del tempo
plug-in disponibili per Eclipse e Idea intellej IDE
link per il Download JProfiler