すでにまとめられているページがあるので、とりあえずそちらをご覧ください。
http://www.syboos.jp/java/doc/how-to-do-when-OutOfMemoryError.html
JVMTI を利用したプロファイラエージェントで、ヒープや実行時間のプロファイリングなど基本的な機能が提供されている。
これでも利用できますが、よりビジュアルに結果を見るために、eclipseでMemory Analayzer のプラグインが提供されています。
下記のコマンドでヘルプを見ることができる
java -agentlib:hprof=help
HPROF: Heap and CPU Profiling Agent (JVMTI Demonstration Code)
hprof usage: java -agentlib:hprof=[help]|[<option>=<value>, ...]
Option Name and Value Description Default --------------------- ----------- ------- heap=dump|sites|all heap profiling all cpu=samples|times|old CPU usage off monitor=y|n monitor contention n format=a|b text(txt) or binary output a file=<file> write data to file java.hprof[{.txt}] net=<host>:<port> send data over a socket off depth=<size> stack trace depth 4 interval=<ms> sample interval in ms 10 cutoff=<value> output cutoff point 0.0001 lineno=y|n line number in traces? y thread=y|n thread in traces? n doe=y|n dump on exit? y msa=y|n Solaris micro state accounting n force=y|n force output to <file> y verbose=y|n print messages about dumps y
gc_okay=y|n
- Get sample cpu information every 20 millisec, with a stack depth of 3: java -agentlib:hprof=cpu=samples,interval=20,depth=3 classname - Get heap usage information based on the allocation sites: java -agentlib:hprof=heap=sites classname
java -Xrunhprof:[help]|[<option>=<value>, ...] will behave exactly the same as: java -agentlib:hprof=[help]|[<option>=<value>, ...]
it is not an official product or formal part of the JDK.
in a future release.
http://apis.jpn.ph/fswiki/wiki.cgi?page=Java%2Fhprof%28jvmpi%29
http://lenemarix2.blog28.fc2.com/blog-entry-16.html
jps -l
jstack PID
jstat -gc 5756 250 10
プロセス番号を調べてからつかいます。
自分はcygwinとかいれているので、
tasklist | nkf | grep java
とかで調べています。
バイナリファイルで結果を出力する例
jmap -dump:format=b,file=出力ファイルパス プロセス番号
出てきたファイルを見るには
jhat -J-Xmx700m jmapでの出力ファイルパス
以下紹介サイトより抜粋
詳しくは下記の紹介サイトをごらんください。
http://eclipse.org/mat/downloads.php
http://xiangcai.at.webry.info/201009/article_7.html
※現時点では、MATの更新サイトのアドレスがバージョンアップで変更になっていました。 http://download.eclipse.org/mat/1.2/update-site/
http://tlync.hateblo.jp/entry/20111220/1324372308
1.Eclipseのプラグインなので適当にインストール。
2.Eclipseの実行の構成を開いて、引数タブのVM引数に以下のどちらかを設定する。
hprofファイルだけを出力します。
-Xrunhprof:heap=dump,format=b,file=./${project_name}.hprof
hprofファイルも出力されるけど関係ないファイルが全部出力される。
-agentlib:hprof=heap=dump,format=b
3.普通に実行
4.hprofファイルが出力されるので開くと、メモリの使用状態が表示される。