ヒープメモリプロファイリングの忘備録

| コメント(0) | トラックバック(0)

DataSpiderのスクリプトの作り方が悪いとOutOfMemoryが発生することがある。
"大容量データ処理を行う"ようにすれば殆どの問題が解決出来るが、
どこでメモリを消費しているかを把握するために、Java SEに標準で付属している
『プロファイラ hprof』を使用した時のメモです。

使用環境
OS : CentOS 5.0
DataSpider Server 2.3.3 (JRE1.5.0_12)
[Install Directory : /usr/local/DataSpiderServista]

プロファイラ hprof の使い方はヘルプ・オプションで表示できます。

	
   $ /usr/local/DataSpiderServista/jre/bin/java  -agentlib:hprof=help

	     HPROF: Heap and CPU Profiling Agent (JVMTI Demonstration Code)

	hprof usage: java -agentlib:hprof=[help]|[=, ...]

	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=                   write data to file               java.hprof[.txt]
	net=:                   send data over a socket          off
	depth=                  stack trace depth                4
	interval=               sample interval in ms            10
	cutoff=                 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                  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 opt

heap と cpu についてのオプション値は以下です。

Option Name Value  
heap dump ヒープダンプを出力
sites 使用メモリの統計情報を出力
all ダンプと統計情報を出力
cpu samples 定期的にサンプリングし、アクティブなスレッド(メソッド)をカウント
times メソッドのIN/OUTを監視し、メソッドのコール回数をカウント
old timesと同様。出力形式が旧hprofフォーマット


DataSpider起動ファイル「DataSpiderServer.lax」の"lax.nl.java.option.additional" 値に "-agentlib:hprof=heap=sites" を追加する。

	
	$ vi /usr/local/DataSpiderServista/server/bin/DataSpiderServer.lax
	#   LAX.NL.JAVA.LAUNCHER.MAIN.METHOD
	#   --------------------------------
	#   main method of LaunchAnywhere's java launcher -- do not adjust

	lax.nl.java.launcher.main.method=main

	lax.nl.java.option.additional=-server -Djava.ext.dirs=../system/boot/ext:../../jre/lib/ext
           -Djava.endorsed.dirs=../system/boot/endorsed -Djava.system.class.loader=com.appresso.ds.boot.CommonClassLoader
           -Ddataspider.home=.. -Ddataspider.product=DataSpider -Ddataspider.boot.class=com.appresso.ds.boot.main.DataSpiderServerMain
-Ddataspider.boot.type=server -Ddataspider.boot.wait.before.abort=true -XX:MaxPermSize=128M -agentlib:hprof=heap=sites


準備は上記のみです。

その後以下の順に実行し、どのオブジェクトでcpu、heapを使用しているかを確認します。
  1.DataSpider起動
  2.該当処理を実行
  3.DataSpider停止

JavaVM停止する時にファイルへ出力するため、必要な処理が完了したら速やかに DataSpiderを停止します。

以下に、1.DataSpider起動、3.DataSpider停止 のみを実施したログを転記します。
DataSpiderのバージョンやコンポーネントされているアダプタによって値は変わるので、自環境で試してみてください。

	
SITES BEGIN (ordered by live bytes) Wed Mar 31 17:06:28 2010 percent live alloc'ed stack class rank self accum bytes objs bytes objs trace name 1 12.88% 12.88% 1431864 404 17155416 5225 300084 byte[] 2 3.16% 16.04% 351688 1565 481872 2237 301079 char[] 3 1.95% 18.00% 217120 3729 2151096 56116 300008 char[] 4 1.73% 19.72% 191912 6 191912 6 320865 byte[] 5 1.55% 21.27% 172368 21 295488 36 300990 byte[] 6 1.52% 22.79% 168608 1037 168608 1037 310828 char[] 7 1.50% 24.29% 167064 3600 183264 3948 313061 char[] 8 1.39% 25.68% 154184 2167 154360 2169 305531 char[] 9 1.35% 27.03% 150560 1882 295520 3694 301006 java.lang.reflect.Method 10 1.28% 28.31% 142168 2167 142272 2169 305533 char[] 11 1.21% 29.53% 135000 1852 137768 2025 300111 char[] 12 1.05% 30.58% 117280 2365 168144 3352 306615 char[] 13 1.01% 31.60% 112832 3526 197248 6164 300599 java.util.LinkedHashMap$Entry 14 0.78% 32.37% 86400 3600 94752 3948 313060 java.lang.String 15 0.71% 33.08% 78600 362 81256 375 300051 char[] 16 0.66% 33.74% 73704 362 82824 407 300060 char[] 17 0.65% 34.39% 72032 1244 76688 1379 317693 char[] 18 0.64% 35.03% 70616 1769 456624 16523 306611 char[] 19 0.60% 35.62% 66240 2760 66240 2760 306708 java.util.HashMap$Entry 20 0.59% 36.21% 65552 1 65552 1 306560 byte[] 21 0.55% 36.76% 60816 2534 179592 7483 300208 java.util.HashMap$Entry 22 0.51% 37.27% 57192 2383 72648 3027 305542 java.util.HashMap$Entry 23 0.51% 37.79% 57184 792 57184 792 310345 char[] 24 0.51% 38.30% 56712 2363 80448 3352 306614 java.lang.String 25 0.49% 38.78% 54216 2259 133632 5568 300185 java.lang.String :(中略) 557 0.01% 76.89% 1120 35 1120 35 305185 sun.reflect.UnsafeQualifiedStaticIntegerFieldAccessorImpl 558 0.01% 76.90% 1120 35 1120 35 311682 com.appresso.ds.kernel.share.db.DBColumn 559 0.01% 76.91% 1120 28 1120 28 313144 java.util.HashMap 560 0.01% 76.92% 1120 28 1120 28 313226 java.util.HashMap 561 0.01% 76.93% 1120 28 1120 28 313561 java.security.ProtectionDomain[] 562 0.01% 76.94% 1120 28 1120 28 313795 java.util.HashMap 563 0.01% 76.95% 1120 28 1120 28 322676 java.util.HashMap SITES END

rank   消費メモリの順位
percent self 消費メモリのパーセンテージ
accum 消費メモリの累積パーセンテージ(上位rankのpercent selfとの和)
live bytes ライブオブジェクト(GCされていないオブジェクト)の消費バイト数
objs ライブオブジェクトのオブジェクト数
alloc'ed bytes アロケートされたオブジェクト(GCされたオブジェクトを含む)の消費バイト数
objs アロケートされたオブジェクトのオブジェクト数
stack trace   対応するトレース番号
class name   クラス名


尚、注意点として、1.DataSpider起動、3.DataSpider停止 時の使用オブジェクトもカウントされてしまうため、
処理を行わない状態でDataSpiderの起動と停止を実施・比較する必要があります。

トラックバック(0)

トラックバックURL: http://www.terrasky.co.jp/cgi-bin/mt/mt-tb.cgi/137

コメントする