Professional Documents
Culture Documents
DZone Garbage Collection Tutorial 2
DZone Garbage Collection Tutorial 2
Tweet 67 195
Thi s i s the s econd a r�cl e i n the s eri es of "Become a Java GC Expert". In the first i s s ue Understa ndi ng
Java Ga rba ge Col l ec�on we have l ea rned a bout the proces s es for di fferent GC a l gori thms , a bout
how GC works , what Young a nd Ol d Genera�on i s , what you s houl d know a bout the 5 types of GC i n
the new JDK 7, a nd what the performa nce i mpl i ca�ons a re for ea ch of thes e GC types .
In thi s a r�cl e, I wi l l expl a i n how JVM is actually running Garbage Collec�on in the real �me.
What is GC Monitoring?
Garbage Collec�on Monitoring refers to the process of figuring out how JVM is running GC. For exa mpl e,
we ca n find out:
GC moni tori ng i s ca rri ed out to see if JVM is running GC efficiently, a nd to check if addi�onal GC tuning is
necessary. Ba s ed on thi s i nforma�on, the a ppl i ca�on ca n be edi ted or GC method ca n be cha nged
(GC tuning).
There a re di fferent ways to moni tor GC, but the onl y di fference i s how the GC opera�on i nforma�on
i s s hown. GC i s done by JVM, a nd s i nce the GC moni tori ng tool s di s cl os e the GC i nforma�on
provi ded by JVM, you wi l l get the s a me res ul ts no ma�er how you moni tor GC. Therefore, you do not
need to l ea rn a l l methods to moni tor GC, but s i nce i t onl y requi res a l i �l e a mount of �me to l ea rn
ea ch GC moni tori ng method, knowi ng a few of them ca n hel p you us e the ri ght one for di fferent
s i tua�ons a nd envi ronments .
The tool s or JVM op�ons l i sted bel ow ca nnot be us ed uni vers a l l y rega rdl es s of the HVM vendor.
Thi s i s beca us e there i s no need for a "sta nda rd" for di s cl os i ng GC i nforma�on. In thi s exa mpl e we
wi l l us e HotSpot JVM (Ora cl e JVM). Si nce NHN i s us i ng Ora cl e (Sun) JVM, there s houl d be no
di fficul �es i n a ppl yi ng the tool s or JVM op�ons that we a re expl a i ni ng here.
Fi rst, the GC moni tori ng methods ca n be s epa rated i nto CUI a nd GUI dependi ng on the a cces s
i nterfa ce. The typi ca l CUI GC moni tori ng method i nvol ves us i ng a s epa rate CUI a ppl i ca�on ca l l ed
"jstat", or s el ec�ng a JVM op�on ca l l ed "verbosegc" when runni ng JVM.
GUI GC moni tori ng i s done by us i ng a s epa rate GUI a ppl i ca�on, a nd three most commonl y us ed
a ppl i ca�ons woul d be "jcons ol e", "jvi s ua l vm" a nd "Vi s ua l GC".
jstat
jstat i s a moni tori ng tool i n HotSpot JVM. Other moni tori ng tool s for HotSpot JVM a re jps a nd jstatd.
Some�mes , you need a l l three tool s to moni tor a Java a ppl i ca�on.
jstat does not provi de onl y the GC opera�on i nforma�on di s pl ay. It a l s o provi des cl a s s l oa der
opera�on i nforma�on or Just-i n-Ti me compi l er opera�on i nforma�on. Among a l l the i nforma�on
jstat ca n provi de, i n thi s a r�cl e we wi l l onl y cover i ts func�ona l i ty to monitor GC opera�ng
i nforma�on.
jstat i s l ocated i n $JDK_HOME/bi n, s o i f java or javac ca n run wi thout s e�ng a s epa rate di rectory
from the comma nd l i ne, s o ca n jstat.
Just l i ke i n the exa mpl e, the rea l type data wi l l be output a l ong wi th the fol l owi ng col umns :
S0C S1C S0U S1U EC EU OC OU PC.
vmid (Vi rtua l Ma chi ne ID), a s i ts na me i mpl i es , i s the ID for the VM. Java a ppl i ca�ons runni ng
ei ther on a l oca l ma chi ne or on a remote ma chi ne ca n be s peci fied us i ng vmi d. The vmi d for Java
a ppl i ca�on runni ng on a l oca l ma chi ne i s ca l l ed lvmid (Loca l vmi d), a nd us ua l l y i s PID. To find out
the l vmi d, you ca n wri te the PID va l ue us i ng a ps comma nd or Wi ndows ta s k ma na ger, but we
s uggest jps beca us e PID a nd l vmi d does not a l ways match. jps sta nds for Java PS. jps s hows vmids
a nd ma i n method i nforma�on. Just l i ke ps s hows PIDs a nd proces s na mes .
Fi nd out the vmi d of the Java a ppl i ca�on that you wa nt to moni tor by us i ng jps , then us e i t a s a
pa ra meter i n jstat. If you us e jps a l one, onl y bootstra p i nforma�on wi l l s how when s evera l WAS
i nsta nces a re runni ng i n one equi pment. We s uggest that you us e ps -ef | grep java comma nd a l ong
wi th jps.
GC performa nce data needs consta nt obs erva�on, therefore when runni ng jstat, try to output the GC
moni tori ng i nforma�on on a regul a r ba s i s .
For exa mpl e, runni ng "jstat –gc <vmid> 1000" (or 1s ) wi l l di s pl ay the GC moni tori ng data on the
cons ol e every 1 s econd. "jstat –gc <vmid> 1000 10" wi l l di s pl ay the GC moni tori ng i nforma�on once
every 1 s econd for 10 �mes i n tota l .
There a re ma ny op�ons other tha n -gc, a mong whi ch GC rel ated ones a re l i sted bel ow.
Onl y l ooki ng at frequency, you wi l l proba bl y us e -gcu�l (or -gcca us e), -gc a nd -gccapacity the most i n
that order.
-gcu�l i s us ed to check the us a ge of hea p a rea s , the number of GC performed, a nd the tota l
a ccumul ated �me for GC opera�ons ,
whi l e -gccapacity op�on a nd others ca n be us ed to check the a ctua l s i ze a l l ocated.
The a dva nta ge of jstat i s that i t ca n a l ways moni tor the GC opera�on data of Java a ppl i ca�ons
runni ng on l oca l /remote ma chi ne, a s l ong a s a cons ol e ca n be us ed. From thes e i tems , the
fol l owi ng res ul t i s output when –gcu�l i s us ed. At the �me of GC tuni ng, pay ca reful a�en�on to
YGC, YGCT, FGC, FGCT a nd GCT.
Thes e i tems a re i mporta nt beca us e they s how how much �me wa s s pent i n runni ng GC.
In thi s exa mpl e, YGC i s 217 a nd YGCT i s 0.928. So, a�er ca l cul a�ng the a ri thme�ca l avera ge, you ca n
s ee that i t requi red a bout 4 ms (0.004 s econds ) for ea ch young GC. Li kewi s e, the avera ge ful l GC
�me us 33ms.
But the a ri thme�ca l avera ge o�en does not hel p a na l yzi ng the a ctua l GC probl em. Thi s i s due to
the s evere devi a�ons i n GC opera�on �me. (In other words , i f the avera ge �me i s 0.067 seconds for a
ful l GC, one GC may have l a sted 1 ms whi l e the other one l a sted 57 ms.) In order to check the
i ndi vi dua l GC �me i nstea d of the a ri thme�ca l avera ge �me, i t i s be�er to us e -verbosegc.
-verbosegc
-verbosegc i s one of the JVM op�ons s peci fied when runni ng a Java a ppl i ca�on. Whi l e jstat ca n
moni tor a ny JVM a ppl i ca�on that ha s not s peci fied a ny op�ons , -verbosegc needs to be s peci fied i n
the begi nni ng, s o i t coul d be s een a s a n unneces s a ry op�on (s i nce jstat ca n be us ed i nstea d).
However, a s -verbosegc di s pl ays ea sy to understa nd output res ul ts whenever a GC occurs , i t i s very
hel pful for moni tori ng rough GC i nforma�on.
-XX:+Pri ntGCDeta i l s
-XX:+Pri ntGCTi meSta mps
-XX:+Pri ntHea pAtGC
-XX:+Pri ntGCDateSta mps (from JDK 6 update 4)
If onl y -verbosegc i s us ed, then -XX:+PrintGCDetails i s a ppl i ed by defa ul t. Addi �ona l op�ons for
–verbosgc a re not excl us i ve a nd ca n be mi xed a nd us ed together.
When us i ng -verbosegc, you ca n s ee the res ul ts i n the fol l owi ng format whenever a mi nor GC occurs .
[GC [<collector>: <starting occupancy1> -> <ending occupancy1>, <pause time1> secs] <starting occupancy3> -> <ending
If a CMS col l ector i s us ed, then the fol l owi ng CMS i nforma�on ca n be provi ded a s wel l .
As -verbosegc op�on outputs a l og every �me a GC event occurs , i t i s ea sy to s ee the cha nges of the
hea p us a ge rates ca us ed by GC opera�on.
Java Vi s ua l VM i s a GUI profil i ng/moni tori ng tool provi ded by Ora cl e JDK.
Though the ba s i c GC status i s a l s o ava i l a bl e through the ba s i c features of Vi s ua l VM, you ca nnot
a cces s deta i l ed i nforma�on that i s ava i l a bl e from ei ther jstat or -verbosegc op�on.
If you wa nt the deta i l ed i nforma�on provi ded by jstat, then i t i s recommended to i nsta l l the Vi s ua l
GC pl ug-i n.
By us i ng Vi s ua l GC, you ca n s ee the i nforma�on provi ded by runni ng jstatd i n a more i ntui �ve way.
Figure 4: HPJMeter.
In thi s a r�cl e I focus ed on how to monitor GC opera�on informa�on, a s the prepa ra�on sta ge for GC
tuni ng. From my pers ona l experi ence, I s uggest us i ng jstat to moni tor GC opera�on, a nd i f you feel
that i t ta kes too l much �me to execute GC, then try -verbosegc op�on to a na l yze GC. The genera l GC
tuni ng proces s i s to analyze the results a�er applying the changed GC op�ons a�er the -verbosegc op�on
ha s been a ppl i ed ba s ed on the a na l ys i s . In the next a r�cl e, we wi l l s ee the best op�ons for
execu�ng GC tuni ng by us i ng rea l ca s es a s our exa mpl es .
By Sa ngmi n Lee, Seni or Engi neer at Performa nce Engi neeri ng La b, NHN Corpora�on.
See also
The Principles of Java Applica�on Performance Tuning
Dev Pla�orm Thi s i s the fi�h a r�cl e i n the s eri es of "Become a Java GC Expert". In the first i s s ue
Understa ndi ng Java Ga rba ge Col ...
3 years ago by Se Hoon Park 0 109555