0% found this document useful (0 votes)
298 views56 pages

Gargbage Collection (GC) Schemes

The document discusses garbage collection (GC) schemes in Java. It explains that generational GC divides the heap into different generations (young, old, permanent) which are collected using different algorithms tailored to their characteristics. The main types of collectors discussed are the serial collector, parallel collector, parallel compacting collector, and concurrent mark-sweep (CMS) collector. It provides details on how each collector handles GC for both the young and old generations. Key GC performance metrics like throughput and pause time are also covered.

Uploaded by

rkishore20077461
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
298 views56 pages

Gargbage Collection (GC) Schemes

The document discusses garbage collection (GC) schemes in Java. It explains that generational GC divides the heap into different generations (young, old, permanent) which are collected using different algorithms tailored to their characteristics. The main types of collectors discussed are the serial collector, parallel collector, parallel compacting collector, and concurrent mark-sweep (CMS) collector. It provides details on how each collector handles GC for both the young and old generations. Key GC performance metrics like throughput and pause time are also covered.

Uploaded by

rkishore20077461
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Gargbage Collection (GC)

Gargbage Collection (GC)


Schemes
Schemes
Sang Shin
Sang Shin
Technology Evangelist Technology Evangelist
Sun Microsystems, Inc. Sun Microsystems, Inc.
www.javapassion.com www.javapassion.com
1
2
Agenda
Why you care on GC (as a eveloper!"
What is an Why Generational GC"
GC #er$ormance Metrics
GC %lgorithms
Types o$ Collectors
>
Serial collector
>
#arallel collector
>
#arallel compact collector
>
Concurrent Mar&'Sweep (CMS! collector( regular,
incremental
Ergonomics
3
Why you care on GC?
In general, the e$ault setting shoul wor& $ine $or most
applications
)or GC sensitive applications, however, choosing a
wrong GC scheme coul result in a less than esira*le
per$ormance
What is and Why
What is and Why
Generational GC?
Generational GC?
5
Issues of Non-Generational GC
Most straight$orwar GC will just iterate over every
o*ject in the heap an etermine i$ any other
o*jects re$erence it
>
This is the *ehavior o$ +on'generational GC
>
This gets really slow as the num*er o$ o*jects in the
heap increases
>
This oes not ta&e avantage o$ the characteristics o$
typical o*jects
,ence the reason $or Generational GC
6
Behavior of Typical Java O!ects
Typical o*ject (-oung o*ject! is most li&ely to ie
shortly a$ter it was create
>
It is calle .high in$ant mortality/
>
E0ample( 1ocal o*jects
2*jects that have *een aroun $or a while (2l
o*jects! will li&ely stay aroun $or a while
>
E0ample( 2*jects initiali3e at the time o$ application
startup
2nly a $ew re$erences $rom ol o*jects to young
o*jects e0ist
7
"#pirical $tatistics
Most o*jects are very short live
>
45'647 o$ all newly allocate o*jects
ie shortly a$ter they are create
>
45'647 o$ all newly allocate o*jects
ie *e$ore another mega*yte has *een allocate
8
%eap $pace With Generations
,eap space is organi3e into .generations/
>
-oung generation ($or young o*jects!
>
2l (or Tenure! generation ($or ol o*jects!
>
#erm generation (meta ata, classes, etc.!
&oung Generation
'er#anent Generation
Old Generation
9
'rinciples of Generational GC
8eep young an ol o*jects separate
>
Mi0ing them in a single space woul result in ine$$icient
GC
9se i$$erent GC algorithms $or i$$erent generation
heap spaces
>
2*jects in each generation have i$$erent li$e'cycle
tenencies
>
.9se the right tool $or the jo*/
10
Characteristics of &oung Generation
%eap $pace
GC:s occur relatively $re;uently
GC:s are e$$icient an $ast *ecause young
generation space is usually small an li&ely to
contain a lot o$ o*jects that are no longer re$erence
2*jects that survive some num*er o$ young
generation collections are promote, or tenure, to
ol generation heap space
11
Characteristics of Old Generational
%eap $pace
Typically larger than young generation heap space
Its occupancy grows more slowly
GC:s are in$re;uent *ut ta&es signi$icantly longer
time to complete
12
Generational GC
13
&oung Generation $pace (ayout
Mae o$ an area calle Eden plus two smaller survival
spaces
Most o*jects are initially allocate in Een
2ne o$ the two survival spaces hol o*jects that survive at
least one young generation GC while the other is empty
14
GC Algorith#s )sed
-oung generation
>
%lgorithms use emphasi3e spee since young
generation GC:s are $re;uent
2l generation
>
%lgorithms use emphasi3e e$$icient space since ol
generation ta&es up most o$ the heap space an have to
wor& with low gar*age ensities
When *oes GC
When *oes GC
Occur?
Occur?
16
When GC Occurs
When the young generation $ills up, a young
generation GC occurs
>
-oung generation GC is calle minor GC
When the ol generation oes not have enough
space $or the o*jects that are *eing promote, a )ull
GC occurs
>
)ull GC is also calle major GC
GC 'erfor#ance
GC 'erfor#ance
+etric
+etric
18
I#portant GC 'erfor#ance +etric
Throughput
>
The percentage o$ total time not spent in gar*age
collection, consiere over long perios o$ time.
#ause time
>
The length o$ time uring which application e0ecution is
stoppe while gar*age collection is *eing per$orme
19
Application ,e-uire#ent
<i$$erent applications have i$$erent re;uirements
>
,igher throughput is more important $or We* application(
pauses uring gar*age collection may *e tolera*le, or
simply o*scure *y networ& latencies.
>
Shorter pause time is more important to an interactive
graphics application
*e#o.
*e#o.
Collecting GC
Collecting GC
Infor#ation through
Infor#ation through
/isualGC0 !stat
/isualGC0 !stat
GC Algorith#s
GC Algorith#s
22
Choices of GC Algorith#s
Serial vs. #arallel
Stop'the'worl vs. Concurrent
Compacting vs. +on'compacting vs. Copying
23
$erial vs1 'arallel
Serial
>
2ne C#9 hanles GC tas&
#arallel
>
Multiple C#9s hanle GC tas&
simultaneously
CPU 0
CPU 1
CPU 2
CPU 3
CPU 0
CPU 1
CPU 2
CPU 3
24
$top-the-2orld vs1 Concurrent
Stop'the'worl
>
E0ecution o$ the application is completely
suspene uring GC
>
Simpler to implement (pro!
> 1onger pause time (con!
Concurrent
>
2ne or more GC tas&s can *e e0ecute
concurrently with the application
>
Shorter pause time (pro!
>
Some overhea (con!
CPU 0
CPU 1
CPU 2
CPU 3
25
Co#pacting vs1 Non-co#pacting vs1
Copying
Compacting
>
Move all live o*jects together an completely reclaim the
remaining memory
+on'compacting
>
=eleases the space .in'place/
Copying
>
Copies o*jects to a i$$erent memory area
>
The source area is empty an availa*le $or $ast an easy
su*se;uent allocations
Types of
Types of
GC Collector
GC Collector
27
Types of GC Collector
Serial Collector
#arallel Collector
#arallel Compacting Collector
Concurrent Mar&'Sweep (CMS! Collector
$erial Collector
$erial Collector
29
$erial Collector
>oth young generation GC an ol generation GC
are one serially (using a single C#9!
Stop'the'worl
9se $or most applications that
>
are running on client'style machines
>
o not have low pause time re;uirement
<e$ault $or client'style machines in ?ava SE @ A B
Can *e e0plicitly re;ueste with
>
-XX:+UseSerialGC
30
$erial Collector on &oung Generation. Before
1ive o*jects are copie $rom Een to empty survival space
=elatively young live o*jects in the occupie ()rom! survival
space are copie to empty (To! survival space while relatively
ol ones are copie to ol generation space irectly
31
$erial Collector on &oung Generation. After
>oth Een an the $ormerly occupie survival space are
empty
2nly the $ormerly empty survival space contains live o*jects
Survival spaces switch roles
32
$erial Collector on Old Generation
The ol an permanent generations are collecte
via serial mar&'sweep'compact collection algorithm
'arallel Collector
'arallel Collector
34
'arallel Collector
%lso &nown as Throughput Collector
>
9sing multiple C#9s $or young generation GC, thus increases
the throughput
Still stop'the'worl C application is suspene
9se $or ?ava applications which run on machines with a
lot o$ physical memory an multiple C#9s an o not
have low pause time re;uirement
> In$re;uent *ut potentially long ol generation GC can still occur
>
E0amples( >atch applications, *illing, payroll, scienti$ic
computing, etc
Can *e e0plicitly re;ueste with
>
-XX:+UseParallelGC
35
'arallel Collector on &oung Generation
9ses a parallel version o$ young generation collection
algorithm o$ a serial collector
36
'arallel Collector on Old Generation
The ol an permanent generations are collecte
via serial mar&'sweep'compact collection algorithm
(as in the Serial Collector!
'arallel Co#pact
'arallel Co#pact
Collector
Collector
38
'arallel Co#pact Collector
It uses a new algorithm $or ol generation collection
>
<i$$erence $rom the #arallel Collector
Still stop'the'worl
9se $or ?ava applications which run on machines with a
lot o$ physical memory an multiple C#9s an o have
low pause time re;uirement
>
#arallel operations o$ ol generation collection reuces pause
time
Can *e e0plicitly re;ueste with
>
-XX:+UseParallelOldGC
39
'arallel Co#pact Collector on &oung
Generation
It uses the same algorithm $or young generation
collection as #arallel Collector
40
'arallel Co#pact Collector on Old
Generation
Three phases
>
Mar&ing phase
>
Summary phase
>
Compaction phase
Still stop the worl
Concurrent +ar3
Concurrent +ar3
and $2eep 4C+$5
and $2eep 4C+$5
Garage Collector.
Garage Collector.
,egular +ode
,egular +ode
42
C+$ Collector
9se when an application nees shorter pause time an
can a$$or to share processor resources with GC when
application is running
>
%pplications with relatively large set o$ long'live ata (a large
ol generation! an run multi'C#9 machines
>
E0ample( We* server
%lso calle 1ow #ause Collector
Can *e e0plicitly re;ueste with
>
-XX:+UseConcMarkSweepGC
> -XX:ParallelCMSThreads=<n>
43
C+$ Collector on &oung Generation
It uses the same algorithm $or young generation
collection as #arallel Collector
44
C+$ 'hases 4for Old Generation GC5
Initial mar&
>
Stop'the'worl pause to mar& $rom roots
>
+ot a complete mar&ingDonly one level eep
Concurrent mar&
>
Mar& $rom the set o$ o*jects $oun uring Initial Mar&
=emar&
>
Stop'the'worl pause to complete mar&ing cycle
>
Ensures a consistent view o$ the worl
Concurrent Sweep
>
=eclaim ea space, aing it *ac& onto $ree lists
Concurrent =eset
45
C+$ 'hases
Java thread
GC thread
I
n
i
t
i
a
l

M
a
r
k
C
o
n
c
u
r
r
e
n
t

M
a
r
k
R
e
m
a
r
k
C
o
n
c
u
r
r
e
n
t

S
w
e
e
p
R
e

e
t
CPU 0
CPU 1
CPU 2
CPU 3
CPU !
CPU "
CPU #
CPU $
GC active
46
C+$ 2ith 'arallelis#
=emar& is typically the largest pause
>
2$ten larger than -oung GC pauses
>
#arallel remar& availa*le since ?ava @ plat$orm
Single mar&ing threa
>
Can &eep up with EFC4 cpus, usually not more
>
#arallel concurrent mar& availa*le in ?ava B plat$orm
Single sweeping threa
>
1ess o$ a *ottlenec& than mar&ing
>
#arallel concurrent sweep coming soon
47
C+$ 'hases 2ith 'arallelis#
Java thread
GC thread
I
n
i
t
i
a
l

M
a
r
k
C
o
n
c
u
r
r
e
n
t

M
a
r
k
R
e
m
a
r
k
C
o
n
c
u
r
r
e
n
t

S
w
e
e
p
R
e

e
t
CPU 0
CPU 1
CPU 2
CPU 3
CPU !
CPU "
CPU #
CPU $
GC active
48
Concurrent +ar3 $2eep Collector
Scheuling o$ collection hanle *y GC
>
>ase on statistics in ?GM
>
2r 2ccupancy level o$ tenure generation
>'HH(CMSTrigger=atio
>'HH(CMSInitiating2ccupancy)raction
Concurrent +ar3
Concurrent +ar3
and $2eep 4C+$5
and $2eep 4C+$5
Garage Collector.
Garage Collector.
Incre#ental +ode
Incre#ental +ode
50
C+$ Collector in Incre#ental +ode
CMS Collector can *e use in a moe in which the
concurrent phases are one incrementally
>
Meant to lesson the impact o$ long concurrent phases *y
perioically stopping the concurrent phase to yiel *ac&
processing to the application
Can *e e0plicitly re;ueste with
>
-XX:+CMSIncreen!alMode
51
C+$ 'hases
Java thread
GC thread
I
n
i
t
i
a
l

M
a
r
k
C
o
n
c
u
r
r
e
n
t

M
a
r
k

i
n
t
e
r
l
e
a
v
e
d

w
i
t
h

a
p
p
l
i
c
a
t
i
o
n
R
e
m
a
r
k
C
o
n
c
u
r
r
e
n
t

S
w
e
e
p
R
e

e
t
CPU 0
CPU 1
CPU 2
CPU 3
CPU !
CPU "
CPU #
CPU $
GC active
52
Other Incre#ental C+$ Options
'HH(CMSIncremental<utyCycleIJn"> (e$ault( @5!
'HH(CMSIncremental<utyCycleMinIJn"> (e$ault(
K5!
'HH(LCMSIncremental#acing (e$ault( true!
"rgono#ics
"rgono#ics
54
J/+ "rgono#ics
Throughput Collector
Ergonomics ena*les the $ollowing(
>
Throughput gar*age collector an %aptive Si3ing
>
('HH (L9se#arallelGC 'HH(L9se%aptiveSi3e#olicy!
>
Initial heap si3e o$ KMBF o$ physical memory up to KG*yte
>
Ma0imum heap si3e o$ KMF o$ physical memory up to KG*
>
Server runtime compiler ('server!
To ena*le server ergonomics on NO'*it Winows, use
the $ollowing $lags(
>
'server 'Hm0Kg 'HH(L9se#arallelGC
>
Garying the heap si3e.
55
)sing J/+ "rgono#ics
Ma0imum pause time goal
>
'HH(Ma0GC#auseMillisI<nnn>
>
This is a hint, not a guarantee
>
GC will ajust parameters to try an meet goal
>
Can aversely e$$ect application throughput
Throughput goal
>
'HH(GCTime=atioI<nnn>
>
GC Time ( %pplication time I K M (K L nnn!
>
e.g. 'HH(GCTime=atioIK6 (@7 o$ time in GC!
)ootprint goal
>
2nly consiere i$ $irst two goals are met

Gargbage Collection (GC)
Gargbage Collection (GC)
Schemes
Schemes
Sang Shin
Sang Shin
Technology Evangelist Technology Evangelist
Sun Microsystems, Inc. Sun Microsystems, Inc.
www.javapassion.com www.javapassion.com
56

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy