You are on page 1of 8

Garbage

Collectio
n in Java
November 25
2013
Contents
1. Command line Switches..................................................................................... 2
2. Memory ools..................................................................................................... 2
3. Generations....................................................................................................... 3
i. !o"ng Generation............................................................................................... 3
ii. #ld Generation................................................................................................... 3
iii. ermanent Generation....................................................................................... 3
$. Garbage Collection %lgorithms..........................................................................3
i. &he Serial GC..................................................................................................... 3
ii. &he arallel GC................................................................................................... $
iii. &he Conc"rrent Mar' Swee( )CMS* Collector.....................................................5
iv. &he G1 Garbage Collector.................................................................................. +
5. Case st"dy , CS-SS........................................................................................... +
i. ,../0 1seSerialGC21searallelGC21searallel#ldGC.......................................+
ii. ,../01seConcMar'Swee(GC.............................................................................+
iii. ,../01seG1GC................................................................................................... +
1. Command line Switches
Switch Description
,.ms
Sets the initial hea( si3e 4or when the
J5M starts.
%s a general r"le6 set intial hea( si3e ),
.ms* e7"al to the ma8im"m hea( si3e
),.m8* to minimi3e garbage
collections.
,.m8 Sets the ma8im"m hea( si3e.
,.mn Sets the si3e o4 the !o"ng Generation.
,../ermSi3e
Sets the starting si3e o4 the ermanent
Generation.
,../Ma8ermSi3e
Sets the ma8im"m si3e o4 the
ermanent Generation
,../S"rvivor9atio
&he !o"ng generation area is divided
into three s"b,areas/ :den6 and two
s"rvivor s(aces that are e7"al in si3e.
,../NewSi3e
%s a general r"le6 set ,../NewSi3e to
be one,4o"rth the si3e o4 the hea(
si3e. ;ncrease the val"e o4 this o(tion
4or larger n"mbers o4 short,lived
ob<ects.
=e s"re to increase the New
generation as yo" increase the
n"mber o4 (rocessors. Memory
allocation can be (arallel6 b"t garbage
collection is not (arallel.
2. Memory Pools
:den S(ace )hea(*
&he (ool 4rom which memory is initially allocated 4or most ob<ects.
S"rvivor S(ace )hea(*
&he (ool containing ob<ects that have s"rvived the garbage collection o4
the :den s(ace.
&en"red Generation )hea(*
&he (ool containing ob<ects that have e8isted 4or some time in the
s"rvivor s(ace.
ermanent Generation )non,hea(*
&he (ool containing all the re>ective data o4 the virt"al machine itsel46
s"ch as class and method ob<ects. ?ith Java 5Ms that "se class data
sharing6 this generation is divided into read,only and read,write areas.
Code Cache )non,hea(*
&he @otS(ot Java 5M also incl"des a code cache6 containing memory
that is "sed 4or com(ilation and storage o4 native code.
3. Generations
i. Young Generation
&he Young Generation is where all new ob<ects are allocated and aged.
?hen the yo"ng generation Alls "(6 this ca"ses a minor garbage
collection. Minor collections can be o(timi3ed ass"ming a high ob<ect
mortality rate. % yo"ng generation 4"ll o4 dead ob<ects is collected very
7"ic'ly. Some s"rviving ob<ects are aged and event"ally move to the old
generation.
&he Young Generation is where all new ob<ects are allocated and aged.
?hen the yo"ng generation Alls "(6 this ca"ses a minor garbage
collection. Minor collections can be o(timi3ed ass"ming a high ob<ect
mortality rate. % yo"ng generation 4"ll o4 dead ob<ects is collected very
7"ic'ly. Some s"rviving ob<ects are aged and event"ally move to the old
generation.
Stop the World !ent , %ll minor garbage collections are BSto( the
?orldB events. &his means that all a((lication threads are sto((ed "ntil
the o(eration com(letes. Minor garbage collections are always Sto( the
?orld events.
ii. "ld Generation
&he "ld Generation is "sed to store long s"rviving ob<ects. &y(ically6 a
threshold is set 4or yo"ng generation ob<ect and when that age is met6
the ob<ect gets moved to the old generation. :vent"ally the old
generation needs to be collected. &his event is called a major garbage
collection.
Ma<or garbage collection are also Sto( the ?orld events. #4ten a ma<or
collection is m"ch slower beca"se it involves all live ob<ects. So 4or
9es(onsive a((lications6 ma<or garbage collections sho"ld be minimi3ed.
%lso note6 that the length o4 the Sto( the ?orld event 4or a ma<or
garbage collection is aCected by the 'ind o4 garbage collector that is
"sed 4or the old generation s(ace.
iii. Permanent Generation
&he Permanent generation contains metadata re7"ired by the J5M to
describe the classes and methods "sed in the a((lication. &he (ermanent
generation is (o("lated by the J5M at r"ntime based on classes in "se by
the a((lication. ;n addition6 Java S: library classes and methods may be
stored here.Classes may get collected )"nloaded* i4 the J5M Ands they are
no longer needed and s(ace may be needed 4or other classes. &he
(ermanent generation is incl"ded in a 4"ll garbage collection.
#. Gar$age Collection %lgorithms
i. &he Serial GC
&he serial collector is the de4a"lt 4or client style machines in Java S: 5
and +. ?ith the serial collector6 both minor and ma<or garbage collections
are done serially )"sing a single virt"al C1*. ;n addition6 it "ses a mar',
com(act collection method. &his method moves older memory to the
beginning o4 the hea( so that new memory allocations are made into a
single contin"o"s ch"n' o4 memory at the end o4 the hea(. &his
com(acting o4 memory ma'es it 4aster to allocate new ch"n's o4 memory
to the hea(.
Usage Cases
&he Serial GC is the garbage collector o4 choice 4or most a((lications that
do not have low (a"se time re7"irements and r"n on client,style
machines. ;t ta'es advantage o4 only a single virt"al (rocessor 4or
garbage collection wor' )there4ore6 its name*. Still6 on todayDs hardware6
the Serial GC can eEciently manage a lot o4 non,trivial a((lications with
a 4ew h"ndred M=s o4 Java hea(6 with relatively short worst,case (a"ses
)aro"nd a co"(le o4 seconds 4or 4"ll garbage collections*.
%nother (o("lar "se 4or the Serial GC is in environments where a high
n"mber o4 J5Ms are r"n on the same machine )in some cases6 more J5Ms
than available (rocessorsF*. ;n s"ch environments when a J5M does a
garbage collection it is better to "se only one (rocessor to minimi3e the
inter4erence on the remaining J5Ms6 even i4 the garbage collection might
last longer. %nd the Serial GC Ats this trade,oC nicely.
Ginally6 with the (roli4eration o4 embedded hardware with minimal
memory and 4ew cores6 the Serial GC co"ld ma'e a comebac'.
Command Hine Switches
&o enable the Serial Collector "se/
'(()*+seSerialGC
@ere is a sam(le command line
,a!a '(m-12m '(ms3m '(mn1m '(()PermSi.e/20m
'(()Ma-PermSi.e/20m '(()*+seSerialGC ',ar demo.,ar
ii. &he Parallel GC
&he (arallel garbage collector "ses m"lti(le threads to (er4orm the yo"ng
genertion garbage collection. =y de4a"lt on a host with N C1s6 the
(arallel garbage collector "ses N garbage collector threads in the
collection. &he n"mber o4 garbage collector threads can be controlled
with command,line o(tions/
,(()ParallelGC&hreads/1desired num$er2
#n a host with a single C1 the de4a"lt garbage collector is "sed even i4
the (arallel garbage collector has been re7"ested. #n a host with two
C1s the (arallel garbage collector generally (er4orms as well as the
de4a"lt garbage collector and a red"ction in the yo"ng generation
garbage collector (a"se times can be e8(ected on hosts with more than
two C1s. &he arallel GC comes in two >avo"rs.
+sage Cases
&he arallel collector is also called a thro"gh("t collector. Since it can "se
m"ltil(le C1s to s(eed "( a((lication thro"gh("t. &his collector sho"ld
be "sed when a lot o4 wor' need to be done and long (a"ses are
acce(table. Gor e8am(le6 batch (rocessing li'e (rinting re(orts or bills or
(er4orming a large n"mber o4 database 7"eries.
'(()*+seParallelGC
?ith this command line o(tion yo" get a m"lti,thread yo"ng generation
collector with a single,threaded old generation collector. &he
o(tion also does single,threaded com(action o4 old generation.
@ere is a sam(le command line
,a!a '(m-12m '(ms3m '(mn1m '(()PermSi.e/20m
'(()Ma-PermSi.e/20m '(()*+seParallelGC ',ar demo.,ar
'(()*+seParallel"ldGC
With the '(()*+seParallel"ldGC option3 the GC is $oth a
multithreaded young generation collector and multithreaded old
generation collector. 4t is also a multithreaded compacting
collector. 5otSpot does compaction only in the old generation.
Young generation in 5otSpot is considered a copy collector6
there7ore3 there is no need 7or compaction.Compacting descri$es
the act o7 mo!ing o$,ects in a way that there are no holes
$etween o$,ects. %7ter a gar$age collection sweep3 there may $e
holes le7t $etween li!e o$,ects. Compacting mo!es o$,ects so
that there are no remaining holes. 4t is possi$le that a gar$age
collector $e a non'compacting collector. &here7ore3 the di8erence
$etween a parallel collector and a parallel compacting collector
could $e the latter compacts the space a7ter a gar$age collection
sweep. &he 7ormer would not.
5ere is a sample command line
,a!a '(m-12m '(ms3m '(mn1m '(()PermSi.e/20m
'(()Ma-PermSi.e/20m '(()*+seParallel"ldGC ',ar
demo.,ar
iii. &he Concurrent Mar9 Sweep :CMS; Collector
The Concurrent Mark Sweep (CMS) collector (also referred to as the concurrent low pause
collector) collects the tenured generation. It attempts to minimize the pauses due to garbage
collection by doing most of the garbage collection work concurrently with the application
threads. ormally the concurrent low pause collector does not copy or compact the li!e
ob"ects. # garbage collection is done without mo!ing the li!e ob"ects. If fragmentation
becomes a problem$ allocate a larger heap.
Note: CMS collector on young generation uses the same algorithm as that of the parallel
collector.
Usage Cases
The CMS collector should be used for applications that re%uire low pause times and can
share resources with the garbage collector. &'amples include desktop (I application that
respond to e!ents$ a webser!er responding to a re%uest or a database responding to %ueries.
Command Line Switches
To enable the CMS Collector use)
'(()*+seConcMar9SweepGC
and to set the n"mber o4 threads "se '(()ParallelCMS&hreads/1n2
@ere is a sam(le command line
,a!a '(m-12m '(ms3m '(mn1m '(()PermSi.e/20m
'(()Ma-PermSi.e/20m '(()*+seConcMar9SweepGC
'(()ParallelCMS&hreads/2 ',ar demo.,ar
i!. &he G1 Gar$age Collector
The *arbage +irst or *, garbage collector is a!ailable in -a!a . and is designed to be the
long term replacement for the CMS collector. The *, collector is a parallel$ concurrent$ and
incrementally compacting low/pause garbage collector that has %uite a different layout from
the other garbage collectors described pre!iously. 0owe!er$ detailed discussion is beyond
the scope of this 12&.
Command <ine Switches
&o enable the G1 Collector "se/
,../01seG1GC
0ere is a sample command line
,a!a '(m-12m '(ms3m '(()*+seG1GC ',ar demo.,ar
=. Case study ' CSDSS
'(ms>g '(m->g '(()PermSi.e/2=?m '(()Sur!i!or@atio/3
i. '(()* +seSerialGCA+seParallelGCA+seParallel"ldGC

#bservations
1. :den S(ace was grad"ally growing and GC was very slow.
2. Garbage Collector sto((ed when :den S(ace reached its ma8im"m
limit "ltimately leading to a((lication crash.
3. %((lication ran a((ro8imately abo"t 3 ho"rs.
ii. '(()*+seConcMar9SweepGC
#bservations
1. :den s(ace was growing very slow.
2. ?henever :den,S(ace reached its ma8im"m limit6 GC cleaned "(
aro"nd 30I o4 (ermanent generation ob<ects.
3. GC sto((ed wor'ing a4ter a((ro8imately 1J ho"rs "ltimately
leading to a((lication crash.
iii. '(()*+seG1GC
#bservations
1. %((lication still wor'ing even a4ter $J ho"rs contin"es test r"n and
res"lts are still "nder observation.

You might also like