LoginSignup
69
67

More than 5 years have passed since last update.

JavaのGCに関するオプションについてまとめてみた。

Last updated at Posted at 2014-12-01

JavaのGCに関するオプションについてまとめてみた。

GC(ガベージコレクション)が影響する問題に遭遇したので改めてオプションについて調べたことをまとめる。今回はTomcatのJavaOptionsとして使用することを前提とする。

確認環境

  • WindowsServer2003, WindowsServer2008
  • JDK-1.5.0, JDK-1.6.0, JDK-1.7.0
  • Tomcat6.0.37

jconsoleを使用するためのオプション。

  • -Dcom.sun.management.jmxremote=true
  • -Dcom.sun.management.jmxremote.port="AccessPortNumber"
  • -Dcom.sun.management.jmxremote.ssl=false
  • -Dcom.sun.management.jmxremote.authenticate=false

ヒープ領域の定義。(Xmx:最大値、Xms:最小値(初期値))

  • -Xmx10M
  • -Xms10M

Perm領域の定義。(MaxPermSize:最大値、PermSize:最小値(初期値))

  • -XX:PermSize=16M
  • -XX:MaxPermSize=16M

GCログの有効化。Xloggcオプションで指定しない場合は、stdoutに出力される。

  • -verbose:gc

-- Example Logs--
[GC 6710K->2723K(15872K), 0.0034669 secs]
[Full GC 7758K->4566K(15872K), 0.0305567 secs]
-- End --

出力したGCログを指定したファイルへ出力する。verbose:gcオプションと合わせて使用する。

  • -Xloggc:.¥gc.log

-- File Path--
%CATALINA_HOME%¥gc.log
-- End --

GCログの詳細表示。verbose:gcと合わせて使用する。領域別のメモリ解放量が確認できる。

  • -XX:+PrintGCDetails

-- Example Logs--
[GC [DefNew: 4922K->469K(4928K), 0.0043205 secs] 5775K->1810K(15872K),0.0043404 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (System) [Tenured: 4150K->4393K(10944K), 0.0284990 secs] 6842K->4393K(15872K), [Perm : 12740K->12740K(16384K)], 0.0285330 secs] [Times: user=0.03 sys=0.00, real=0.03 secs]
-- End --

DateStampの追加。日付と時間が追加される。(但し、JDK−1.5.0では使用できない。)

  • -XX:+PrintGCDateStamps (with -verbose:gc)

-- Example Logs--
2014-11-27T13:09:31.439+0900: [GC 5281K->1338K(15872K), 0.0043765 secs]
2014-11-27T13:10:38.447+0900: [Full GC 6848K->4529K(15872K), 0.0306083 secs]
-- End --

TimeStampの追加。JVM起動時を0.000secとしての経過時間が追加される。

  • -XX:+PrintGCTimeStamps (with -verbos:gc)

-- Example Logs--
1.691: [GC 7598K->3378K(15872K), 0.0027237 secs]
33.621: [Full GC 6513K->4486K(15872K), 0.0311048 secs]
-- End --

OutOfMemoryErrorが発生したときにDumpFileを出力する。

  • -XX:+HeapDumpOnOutOfMemoryError

-- Example Logs-- (stdout.log)
java.lang.OutOfMemoryError: PermGen space
Dumping heap to java_pid2672.hprof ...
Heap dump file created [10951592 bytes in 0.156 secs]
-- End --

-- File Path--
%CATALINA_HOME%¥java_pid2672.hprof
-- End --

Heap領域に格納されているオブジェクトの統計情報を出力する。※プロセス終了時に出力される。

  • -XX:+PrintClassHistogram

-- Example Logs --
Heap
def new generation total 4928K, used 1112K [0x04400000, 0x04950000, 0x04950000)
eden space 4416K, 20% used [0x04400000, 0x044e7ad0, 0x04850000)
from space 512K, 36% used [0x048d0000, 0x048fe6b0, 0x04950000)
to space 512K, 0% used [0x04850000, 0x04850000, 0x048d0000)
tenured generation total 10944K, used 4393K [0x04950000, 0x05400000, 0x05400000)
the space 10944K, 40% used [0x04950000, 0x04d9a418, 0x04d9a600, 0x05400000)
compacting perm gen total 16384K, used 13204K [0x05400000, 0x06400000, 0x06400000)
the space 16384K, 80% used [0x05400000, 0x060e51e0, 0x060e5200, 0x06400000)
No shared spaces configured.
-- End --

GCごとのHeap領域使用状況を出力する。※大量のログが出力されるため注意。

  • -XX:+PrintHeapAtGC

-- Example Logs --
{Heap before GC invocations=6 (full 0):
def new generation total 2432K, used 2403K [0x36650000, 0x368f0000, 0x368f0000)
eden space 2176K, 98% used [0x36650000, 0x36868e10, 0x36870000)
from space 256K, 100% used [0x36870000, 0x368b0000, 0x368b0000)
to space 256K, 0% used [0x368b0000, 0x368b0000, 0x368f0000)
tenured generation total 5504K, used 3520K [0x368f0000, 0x36e50000, 0x36e50000)
the space 5504K, 63% used [0x368f0000, 0x36c602c8, 0x36c60400, 0x36e50000)
compacting perm gen total 16384K, used 4381K [0x36e50000, 0x37e50000, 0x37e50000)
the space 16384K, 26% used [0x36e50000, 0x37297588, 0x37297600, 0x37e50000)
ro space 10240K, 41% used [0x37e50000, 0x38282050, 0x38282200, 0x38850000)
rw space 12288K, 52% used [0x38850000, 0x38e96800, 0x38e96800, 0x39450000)
0.633: [GC 5924K->4343K(7936K), 0.0031843 secs]
Heap after GC invocations=7 (full 0):
def new generation total 2432K, used 256K [0x36650000, 0x368f0000, 0x368f0000)
eden space 2176K, 0% used [0x36650000, 0x36650000, 0x36870000)
from space 256K, 100% used [0x368b0000, 0x368f0000, 0x368f0000)
to space 256K, 0% used [0x36870000, 0x36870000, 0x368b0000)
tenured generation total 5504K, used 4087K [0x368f0000, 0x36e50000, 0x36e50000)
the space 5504K, 74% used [0x368f0000, 0x36cedda0, 0x36cede00, 0x36e50000)
compacting perm gen total 16384K, used 4381K [0x36e50000, 0x37e50000, 0x37e50000)
the space 16384K, 26% used [0x36e50000, 0x37297588, 0x37297600, 0x37e50000)
ro space 10240K, 41% used [0x37e50000, 0x38282050, 0x38282200, 0x38850000)
rw space 12288K, 52% used [0x38850000, 0x38e96800, 0x38e96800, 0x39450000)
}
-- End --

OLD領域のオブジェクト割り当てと世代数を出力する。※FullGCは世代数関係なしにメモリ開放するため、詳細出力なし。

  • -XX:+PrintTenuringDistribution

-- Example Logs --
Desired survivor size 262144 bytes, new threshold 1 (max 10)
- age 1: 496240 bytes, 496240 total
- age 2: 9408 bytes, 505648 total
- age 3: 18640 bytes, 524288 total
12938K->9115K(15872K), 0.0023662 secs]
368.522: [GC
Desired survivor size 262144 bytes, new threshold 1 (max 10)
- age 1: 524280 bytes, 524280 total
13531K->9546K(15872K), 0.0022748 secs]
368.659: [GC
Desired survivor size 262144 bytes, new threshold 1 (max 10)
- age 1: 524288 bytes, 524288 total
13962K->10267K(15872K), 0.0019545 secs]
368.728: [Full GC 14668K->10004K(15872K), 0.0353300 secs]
-- End --

GCログをローテーションする。(但し、JDK-1.6.0-update34またはJDK-1.7.0-update2以上から有効)

  • -XX:+UseGCLogFileRotation

-- Example Files --
gc.log.0
gc.log.1
...
-- End --

ローテーションファイルの世代数を指定する。UseGCLogFileRotationオプションと合わせて使用する。

  • -XX:NumberOfGCLogFiles=N

ローテーションファイルの閾値を指定する。UseGCLogFileRotationオプションと合わせて使用する。単位は、バイト。(k:キロ、M:メガ、G:ギガ)

  • -XX:GCLogFileSize=N
69
67
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
69
67