hprofとは?
アプリケーションのパフォーマンスチューニングなどに使用するツールとしてプロファイラがある。最近は、Eclipse の TPTP や NetBeans Profiler などフリーのプロファイラがいくつかある。
Java SE にも標準で hprof というプロファイラが付属している。hprofは、J2SE 1.4.2 までは JVMPI (Java1.5は非推奨、Java6からは廃止)を、J2SE 5.0 以降は JVMTI を利用したプロファイラエージェントで、ヒープや実行時間のプロファイリングなど基本的な機能が提供されている。
hprof を利用するには、J2SE 1.4.2 までは「-Xrunhprof」起動オプションを、J2SE 5.0 以降は「-agentlib:hprof」という起動オプションを指定しJavaアプリケーションを実行する。また、JDK6 からは、hprof の結果を扱いやすくするためのツール jhat が付属されるようになった(注:JRE6には付属しない)。
起動オプション
使い方はヘルプ・オプションで表示できる。(1.4.2 までの「-Xrunhprof」は Java/hprof(jvmpi) を参照)
D:\>java -version
java version "1.6.0_11"
Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing)
D:\>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
Obsolete Options
----------------
gc_okay=y|n
Examples
--------
- 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
Notes
-----
- The option format=b cannot be used with monitor=y.
- The option format=b cannot be used with cpu=old|times.
- Use of the -Xrunhprof interface can still be used, e.g.
java -Xrunhprof:[help]|[<option>=<value>, ...]
will behave exactly the same as:
java -agentlib:hprof=[help]|[<option>=<value>, ...]
Warnings
--------
- This is demonstration code for the JVMTI interface and use of BCI,
it is not an official product or formal part of the JDK.
- The -Xrunhprof interface will be removed in a future release.
- The option format=b is considered experimental, this format may change
in a future release.
D:\>
hprofでプロファイル
基本は一緒なので Java/hprof(jvmpi) を参照
jhatでプロファイル解析
jhatは、バイナリ形式のプロファイルを使用する。デフォルトではテキスト形式になるので「format=b」オプションを指定する。
C:\>java -agentlib:hprof=format=b foo
デフォルトではプロファイルは、java.hprof という名前で出力される。jhatは、プロファイルを引数にして起動する。
C:\>jhat java.hprof
jhatはWebサーバーとして動作し、Webブラウザで解析結果を見る。デフォルトのポート番号は7000なので http://localhost:7000/ にアクセスする。ポート番号は-portオプションで変更できる。
トップページにアクセスするとクラスの一覧が表示される。ただし、ランタイムのクラスは含まれず、アプリケーションのクラスのみが表示される。ここのクラスのリンクを辿ると、そのクラスに関する情報が表示される。参照関係も含まれているので、参照されているor参照しているクラスが確認できる。
トップページの最下部の Show heap histogram というリンクを辿ると、ヒープ・ヒストグラムが表示される。
トップページ最下部にある Show instance counts for all classes からインスタンスの一覧が表示される。このinstanceのリンクをたどると、そのクラスのインスタンスの一覧が表示される。さらに、それぞれのインスタンスを選ぶとそのインスタンスのフィールドの情報やスタック・トレースが表示される。
最終更新時間:2008年12月23日 01時49分00秒 指摘や意見などあればSandBoxのBBSへ。