응용 프로그램에서 사용중인 현재 힙 크기를 보는 방법은 무엇입니까?
-
19-09-2019 - |
문제
구성이 다음과 같이 보이도록 변경 한 이후 NetBeans에서 힙 크기를 1GB로 늘 렸다고 생각합니다.
netbeans_default_options="-J-Xmx1g ......
NetBeans를 다시 시작한 후, 내 앱이 지금 1GB를 받는지 확인할 수 있습니까?
이것을 확인하는 방법이 있습니까?
해결책
이 코드 사용 :
// 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 (대부분의 JDKS가있는 표준)을 사용하여 모든 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));
}
}
Sun Java 6 JDK에서 jvisualvm에 부착하십시오. 시작 플래그가 나열됩니다.
도구를 사용할 수 있습니다 : Eclipse Memory Analyzer Tool http://www.eclipse.org/mat/ .
매우 유용합니다.
JvisualVM이 과잉 일 때 개인적으로 좋아하거나 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
Java 응용 프로그램 힙 크기 및 CPU 시간을 확인하려면 JVM에 액세스하고 모든 힙 및 CPU 시간 할당을 표시하는 JPROFILER 도구를 사용하십시오.
Eclipse 및 Intellej Idea IDE에 사용할 수 있습니다
다운로드 jprofiler 링크
제휴하지 않습니다 StackOverflow