アプリケーションが使用している現在のヒープサイズを表示する方法?
-
19-09-2019 - |
質問
私はこのように見えるように設定を変更して以来、私は、NetBeansで1ギガバイトに私のヒープサイズを増加させたと考えます:
netbeans_default_options="-J-Xmx1g ......
私は、NetBeansを再起動した後、私は私のアプリは現在1ギガバイトを与えていることを確認することができますか?
これを確認する方法はありますか?
解決
このコードを使用します:
// 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();
それを知っている私には有用であった。
他のヒント
あなたは、任意のJavaプロセスのヒープサイズを確認するには(ほとんどのJDKの標準)をjconsoleを使用することができます。
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 JDK 6からjvisualvmとアタッチ。スタートアップフラグが記載されています。
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をツールをチェックするために、
EclipseとintellejアイデアIDEのために利用可能なプラグイン
ダウンロードJProfilerをのためのリンク
所属していません StackOverflow