You are on page 1of 16

한국어 | Login | Register

About Downloads Documenta�on Dev Zone Community Blog

How to Monitor Java Garbage Collec�on


posted 4 years ago in Dev Pla�orm category by Sangmin Lee

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:

1. when a n object i n young ha s moved to ol d a nd by how much,


2. or when stop-the-worl d ha s occurred a nd for how l ong.

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).

How to Monitor GC?

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".

Let's l ea rn more a bout ea ch method.

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.

You ca n try runni ng the fol l owi ng i n the comma nd l i ne.

1 $> jstat –gc $<vmid$> 1000


2
3 S0C S1C S0U S1U EC EU OC OU PC PU YGC
4 3008.0 3072.0 0.0 1511.1 343360.0 46383.0 699072.0 283690.2 75392.0 41064.3 2540
5 3008.0 3072.0 0.0 1511.1 343360.0 47530.9 699072.0 283690.2 75392.0 41064.3 2540
6 3008.0 3072.0 0.0 1511.1 343360.0 47793.0 699072.0 283690.2 75392.0 41064.3 2540
7
8 $>

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.

Op�on Na me Des cri p�on


It s hows the current s i ze for ea ch hea p a rea a nd i ts current us a ge (Ede, s urvi vor,
gc ol d, etc.), tota l number of GC performed, a nd the a ccumul ated �me for GC
opera�ons .
It s hows the mi ni mum s i ze (ms ) a nd maxi mum s i ze (mx) of ea ch hea p a rea ,
gcca pa c�y current s i ze, a nd the number of GC performed for ea ch a rea . (Does not s how
current us a ge a nd a ccumul ated �me for GC opera�ons .)
It s hows the "i nforma�on provi ded by -gcu�l " + rea s on for the l a st GC a nd the
gcca us e
rea s on for the current GC.
gcnew Shows the GC performa nce data for the new a rea .
gcnewca pa ci ty Shows sta�s�cs for the s i ze of new a rea .
gcol d Shows the GC performa nce data for the ol d a rea .
gcol dca pa ci ty Shows sta�s�cs for the s i ze of ol d a rea .
gcpermca pa ci ty Shows sta�s�cs for the perma nent a rea .
Shows the us a ge for ea ch hea p a rea i n percenta ge. Al s o s hows the tota l number
gcu�l
of GC performed a nd the a ccumul ated �me for GC opera�ons .

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.

You ca n s ee the fol l owi ng output by us i ng the -gc op�on:

1 S0C S1C … GCT


2 1248.0 896.0 … 1.246
3 1248.0 896.0 … 1.246
4 … … … …
Di fferent jstat op�ons s how di fferent types of col umns , whi ch a re l i sted bel ow. Ea ch col umn
i nforma�on wi l l be di s pl ayed when you us e the "jstat op�on" l i sted on the ri ght.

Col umn Des cri p�on Jstat Op�on


-gc
-gcca pa ci ty
S0C Di s pl ays the current s i ze of Survi vor0 a rea i n KB
-gcnew
-gcnewca pa ci ty
-gc
-gcca pa ci ty
S1C Di s pl ays the current s i ze of Survi vor1 a rea i n KB
-gcnew
-gcnewca pa ci ty
-gc
S0U Di s pl ays the current us a ge of Survi vor0 a rea i n KB
-gcnew
-gc
S1U Di s pl ays the current us a ge of Survi vor1 a rea i n KB
-gcnew
-gc
-gcca pa ci ty
EC Di s pl ays the current s i ze of Eden a rea i n KB
-gcnew
-gcnewca pa ci ty
-gc
EU Di s pl ays the current us a ge of Eden a rea i n KB
-gcnew
-gc
-gcca pa ci ty
OC Di s pl ays the current s i ze of ol d a rea i n KB
-gcol d
-gcol dca pa ci ty
-gc
OU Di s pl ays the current us a ge of ol d a rea i n KB
-gcol d
-gc
-gcca pa ci ty
PC Di s pl ays the current s i ze of perma nent a rea i n KB -gcol d
-gcol dca pa ci ty
-gcpermca pa ci ty
-gc
PU Di s pl ays the current us a ge of perma nent a rea i n KB
-gcol d
-gc
-gcca pa ci ty
-gcnew
YGC The number of GC event occurred i n young a rea -gcnewca pa ci ty
-gcol d
-gcol dca pa ci ty
Col umn Des cri p�on Jstat Op�on
-gcpermca pa ci ty
-gcu�l
-gcca us e
-gc
-gcnew
YGCT The a ccumul ated �me for GC opera�ons for Yong a rea
-gcu�l
-gcca us e
-gc
-gcca pa ci ty
-gcnew
-gcnewca pa ci ty
FGC The number of ful l GC event occurred -gcol d
-gcol dca pa ci ty
-gcpermca pa ci ty
-gcu�l
-gcca us e
-gc
-gcol d
-gcol dca pa ci ty
FGCT The a ccumul ated �me for ful l GC opera�ons
-gcpermca pa ci ty
-gcu�l
-gcca us e
-gc
-gcol d
-gcol dca pa ci ty
GCT The tota l a ccumul ated �me for GC opera�ons
-gcpermca pa ci ty
-gcu�l
-gcca us e
-gcca pa ci ty
NGCMN The mi ni mum s i ze of new a rea i n KB
-gcnewca pa ci ty
-gcca pa ci ty
NGCMX The maxi mum s i ze of max a rea i n KB
-gcnewca pa ci ty
-gcca pa ci ty
NGC The current s i ze of new a rea i n KB
-gcnewca pa ci ty
-gcca pa ci ty
OGCMN The mi ni mum s i ze of ol d a rea i n KB
-gcol dca pa ci ty
-gcca pa ci ty
OGCMX The maxi mum s i ze of ol d a rea i n KB
-gcol dca pa ci ty
-gcca pa ci ty
OGC The current s i ze of ol d a rea i n KB
-gcol dca pa ci ty
Col umn Des cri p�on Jstat Op�on
-gcca pa ci ty
PGCMN The mi ni mum s i ze of perma nent a rea i n KB
-gcpermca pa ci ty
-gcca pa ci ty
PGCMX The maxi mum s i ze of perma nent a rea i n KB
-gcpermca pa ci ty
-gcca pa ci ty
PGC The current s i ze of perma nent genera�on a rea i n KB
-gcpermca pa ci ty
-gcca pa ci ty
PC The current s i ze of perma nent a rea i n KB
-gcpermca pa ci ty
-gc
PU The current us a ge of perma nent a rea i n KB
-gcol d
LGCC The ca us e for the l a st GC occurrence -gcca us e
GCC The ca us e for the current GC occurrence -gcca us e
Tenuri ng thres hol d. If copi ed thi s a mount of �mes i n young a rea (S0 ->S1,
TT -gcnew
S1->S0), they a re then moved to ol d a rea .
Maxi mum Tenuri ng thres hol d. If copi ed thi s a mount of �mes i ns i de
MTT -gcnew
young a ra e, then they a re moved to ol d a rea .
DSS Adequate s i ze of s urvi vor i n KB -gcnew

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.

1 S0 S1 E O P YGC YGCT FGC FGCT GCT


2 0.00 66.44 54.12 10.58 86.63 217 0.928 2 0.067 0.995
3 0.00 66.44 54.12 10.58 86.63 217 0.928 2 0.067 0.995
4 0.00 66.44 54.12 10.58 86.63 217 0.928 2 0.067 0.995

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.

jstat -verbos egc


Java a ppl i ca�on runni ng on a ma chi ne that ca n l og i n Onl y when -verbogc wa s
Moni tori ng
to a termi na l , or a remote Java a ppl i ca�on that ca n s peci fied a s a JVM sta r�ng
Ta rget
connect to the network by us i ng jstatd op�on
Si ze of ew a nd ol d a rea
Output Hea p status (us a ge, maxi mum s i ze, number of �mes
before/a�er GC, a nd GC
i nforma�on for GC/�me, etc.)
opera�on �me
Output Ti me Every des i gnated �me Whenever GC occurs
Whenever When tryi ng to obs erve the cha nges of the s i ze of When tryi ng to s ee the
us eful hea p a rea effect of a s i ngl e GC

The fol l owi ngs a re other op�ons that ca n be us ed wi th -verbosegc.

-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

Col l ector Na me of Col l ector Us ed for mi nor gc


sta r�ng
The s i ze of young a rea before GC
occupa ncy1
endi ng
The s i ze of young a rea a�er GC
occupa ncy1
pa us e �me1 The �me when the Java a ppl i ca�on stopped runni ng for mi nor GC
sta r�ng
The tota l s i ze of hea p a rea before GC
occupa ncy3
endi ng
The tota l s i ze of hea p a rea a�er GC
occupa ncy3
Col l ector Na me of Col l ector Us ed for mi nor gc
The �me when the Java a ppl i ca�on stopped runni ng for overa l l hea p GC,
pa us e �me3
i ncl udi ng ma jor GC

Thi s i s a n exa mpl e of -verbosegc output for minor GC:

1 S0 S1 E O P YGC YGCT FGC FGCT GCT


2 0.00 66.44 54.12 10.58 86.63 217 0.928 2 0.067 0.995
3 0.00 66.44 54.12 10.58 86.63 217 0.928 2 0.067 0.995
4 0.00 66.44 54.12 10.58 86.63 217 0.928 2 0.067 0.995

Thi s i s the exa mpl e of output res ul ts a�er a n Full GC occurred.

1 [Full GC [Tenured: 3485K->4095K(4096K), 0.1745373 secs] 61244K->7418K(63104K), [Perm : 10756K->10756K(12288K)],

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) VisualVM + Visual GC

Java Vi s ua l VM i s a GUI profil i ng/moni tori ng tool provi ded by Ora cl e JDK.

Figure 1: VisualVM Screenshot.


Instea d of the vers i on that i s i ncl uded wi th JDK, you ca n downl oa d Vi s ua l VM di rectl y from i ts
webs i te. For the s a ke of conveni ence, the vers i on i ncl uded wi th JDK wi l l be referred to a s Java
Vi s ua l VM (jvi s ua l vm), a nd the vers i on ava i l a bl e from the webs i te wi l l be referred to a s Vi s ua l VM
(vi s ua l vm). The features of the two a re not exa ctl y i den�ca l , a s there
a re s l i ght di fferences , s uch a s when i nsta l l i ng pl ug-i ns . Pers ona l l y, I prefer the Vi s ua l VM vers i on,
whi ch ca n be downl oa ded from the webs i te.
A�er runni ng Vi s ua l VM, i f you s el ect the a ppl i ca�on that you wi s h to moni tor from the wi ndow on
the l e� s i de, you ca n find the "Monitoring" ta b there. You ca n get the ba s i c i nforma�on a bout GC a nd
Hea p from thi s Moni tori ng ta b.

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.

Vi s ua l GC ca n be a cces s ed i n rea l �me from the Tools menu.

Figure 2: Viusal GC Installa�on Screenshot.

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.

What is the Next Ar�cle About?

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

How to Tune Java Garbage Collec�on


Dev Pla�orm Thi s i s the thi rd 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�...
4 years ago by Sangmin Lee 0 229488

MaxClients in Apache and its effect on Tomcat during Full GC


Dev Pla�orm Thi s i s the fourth 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 ect...
4 years ago by Dongsoon Choi 0 48747

How Statement Pooling in JDBC affects the Garbage Collec�on


Dev Pla�orm There a re va ri ous techni ques to i mprove the performa nce of your Java a ppl i ca�on. In
thi s a r�cl e I wi l l ta l k a bout Statement ...
4 years ago by Dongsun Choi 2 42264
Understanding Java Garbage Collec�on
Dev Pla�orm What a re the benefits of knowi ng how ga rba ge col l ec�on (GC) works i n Java ? Sa�sfyi ng
the i ntel l ectua l curi os i ty a s a s o�...
4 years ago by Sangmin Lee 6 352000
10 Comments CUBRID Open Source Database Community 
1 Login

 Recommend 2 ⤤ Share Sort by Best

Join the discussion…

denzo • 2 y ears ago


You ca n ta ke a look a t Spy Gla ss Ga r ba g e Collection A n a ly zer to m on itor Ga r ba g e
Collection a ctiv ity in r ea ltim e. It is a fr ee V isu a lV M plu g in .
2 • Reply • Share ›

Marlon Patrick • 3 y ears ago


V er y g ood!
1 • Reply • Share ›

Chandan • 4 months ago


Does WA S m ea n s Websph er e A pplica tion Ser v er h er e?
• Reply • Share ›

Performance expert • 8 months ago


For fr ee g c log a n a ly sis, sen d th e log file to per for m a n cetestex per t@g m a il.com
• Reply • Share ›

Rechly parker • a y ear ago


pr om in en t blog , I a ppr ecia te th is.
• Reply • Share ›

Sw aptan Cdui • a y ear ago


Hi, m a y be a skin g a du m b qu estion . Bu t I a m h a v in g pr oblem w ith ex ecu tin g th e
jsta t com m a n d.

My Ja v a a pplica tion pr ocess id is 3 6 8 8 (pid fr om w in dow s ta sk m a n a g er ). loca l


m a ch in e h ost n a m e is 'W2 K8 -R2 -6 4 bit'.
I r a n com m a n d 'jsta t -g c 3 6 8 8 @W2 K8 -R2 -6 4 bit 1 000' to g et th e er r or : RMI Reg istr y
n ot a v a ila ble a t W2 K8 -R2 -6 4 bit:1 09 9 Con n ection r efu sed to h ost: W2 K8 -R2 -6 4 bit;
n ested ex ception is: ja v a .n et.Con n ectEx ception : Con n ection r efu sed: con n ect.
Th en a fter som e n et su r fin g g ot com m a n d 'sta r t r m im r eg istr y '. Ex ecu ted th is fr om
com m a n d pr om pt. It open ed a n oth er bla n k com m a n d pr om pt.
Th en fr om fir st com m a n d pr om pt r a n sa m e com m a n d 'jsta t -g c 3 6 8 8 @W2 K8 -
R2 -6 4 bit 1 000' to g et th e er r or : RMI Ser v er JSta tRem oteHost n ot a v a ila ble.

Not su r e h ow to solv e it. Plea se h elp.


• Reply • Share ›
About CUBRID | Contact us |

© 2012 CUBRID.org. All rights reserved.

You might also like