Taming Java Garbage Collector

23
Taming the Taming the Java Garbage Java Garbage Collector Collector Daya Daya Atapattu Atapattu

description

These slides may not be self explanatory. Updated for the benefit of attendees of Colombo Java Meetup keynote.

Transcript of Taming Java Garbage Collector

Page 1: Taming Java Garbage Collector

Taming the Taming the Java Garbage Java Garbage

CollectorCollector

Daya Daya AtapattuAtapattu

Page 2: Taming Java Garbage Collector
Page 3: Taming Java Garbage Collector

Why Learn Garbage Collection?Why Learn Garbage Collection?

To select a GC algorithmTo select a GC algorithm

To performance debug applicationsTo performance debug applications

To become a better programmerTo become a better programmer

Page 4: Taming Java Garbage Collector

SummarySummary

How Java garbage collectors workHow Java garbage collectors work

How to monitor and tune GCHow to monitor and tune GC

How to write GC friendly Java codeHow to write GC friendly Java code

Page 5: Taming Java Garbage Collector

Dynamic Memory AllocationDynamic Memory Allocation

Fortran77: No Dynamic Memory Fortran77: No Dynamic Memory

C: Dynamic Memory managed by C: Dynamic Memory managed by programmer. malloc() / free() programmer. malloc() / free()

Java: Dynamic Memory managedJava: Dynamic Memory managed by JVM, i.e. Java GCby JVM, i.e. Java GC

Page 6: Taming Java Garbage Collector
Page 7: Taming Java Garbage Collector

Weak Generational HypothesisWeak Generational Hypothesis

Most objects die young. Most objects die young.

Only few references from old objects Only few references from old objects to young objects.to young objects.

Page 8: Taming Java Garbage Collector

All Apples in One Basket?All Apples in One Basket?

Page 9: Taming Java Garbage Collector

GC RegionsGC Regions

Page 10: Taming Java Garbage Collector

Young Generation CollectionYoung Generation Collection

Page 11: Taming Java Garbage Collector

Old Gen and CompactionOld Gen and Compaction

Page 12: Taming Java Garbage Collector

Stop The World (STW)Stop The World (STW)

Page 13: Taming Java Garbage Collector

Concurrent Mark-Sweep (CMS)Concurrent Mark-Sweep (CMS)

Old gen is collected concurrentlyOld gen is collected concurrently

1.1. STW, Mark rootsSTW, Mark roots

2.2. Mark concurrently with applicationMark concurrently with application

3.3. STW, mark again to catchSTW, mark again to catch upup

4.4. SweepSweep

Page 14: Taming Java Garbage Collector

CMSCMS

Page 15: Taming Java Garbage Collector

CMS CMS ProsPros and and ConsCons

Reduces pauseReduces pause

Does not collect all garbage.Does not collect all garbage.Leaves the heap fragmented so allocation Leaves the heap fragmented so allocation is more expensive.is more expensive.CMS failures. CMS failures. Needs more heap.Needs more heap.Steals threads and CPU from application.Steals threads and CPU from application.

Page 16: Taming Java Garbage Collector

G1 CollectorG1 Collector

Page 17: Taming Java Garbage Collector

GC TuningGC Tuning

Use GC logging options with JVMUse GC logging options with JVM

-Xloggc:gc.log -Xloggc:gc.log

-XX:+PrintGCDetails-XX:+PrintGCDetails

-XX:+PrintGCTimeStamp-XX:+PrintGCTimeStamp

Page 18: Taming Java Garbage Collector

GC TuningGC Tuning

6.255: [GC [PSYoungGen: 132532K-16412K(278912K)]6.255: [GC [PSYoungGen: 132532K-16412K(278912K)] 141408K->25289K(453696K), 0.013 secs]141408K->25289K(453696K), 0.013 secs] [Times: user=0.08 sys=0.01, real=0.01 secs][Times: user=0.08 sys=0.01, real=0.01 secs]

6.269: [Full GC (System)6.269: [Full GC (System) [PSYoungGen: 16412K->0K(278912K)][PSYoungGen: 16412K->0K(278912K)] [PSOldGen: 8876K->24956K(174784K)][PSOldGen: 8876K->24956K(174784K)] 25289K->24956K(453696K)25289K->24956K(453696K) [PSPermGen: 60341K->60341K(120960K)], 0.153 secs][PSPermGen: 60341K->60341K(120960K)], 0.153 secs] [Times: user=0.16 sys=0.00, real=0.16 secs][Times: user=0.16 sys=0.00, real=0.16 secs]

Page 19: Taming Java Garbage Collector

GC MonitoringGC Monitoring

Use tools to visualize GC activityUse tools to visualize GC activity

Some such tools:Some such tools:

JConsoleJConsole

JStatJStat

VisualVM (with GC extension)VisualVM (with GC extension)

Page 20: Taming Java Garbage Collector

Some Important ParametersSome Important Parameters

GC - The garbage collector in use.GC - The garbage collector in use.

Size of the Java heap.Size of the Java heap.

Size of the young and old generation spaces.Size of the young and old generation spaces.

Duration of minor garbage collections.Duration of minor garbage collections.

Frequency of minor garbage collections.Frequency of minor garbage collections.

Duration of full garbage collections.Duration of full garbage collections.

Frequency of full garbage collections.Frequency of full garbage collections.

What initiates full GC; old gen or perm space.What initiates full GC; old gen or perm space.

Page 21: Taming Java Garbage Collector

Attributes of Java PerformanceAttributes of Java Performance

Pause TimePause Time: Minimal pause time due to : Minimal pause time due to GCGC

ThroughputThroughput: Overall performance of user : Overall performance of user process ignoring pauses due to GCprocess ignoring pauses due to GC

FootprintFootprint: Amount of memory required for : Amount of memory required for GC to run efficientlyGC to run efficiently

Page 22: Taming Java Garbage Collector

GC Friendly CodingGC Friendly Coding

GCs love small, immutable, short-lived objectsGCs love small, immutable, short-lived objectsObject pooling is usually badObject pooling is usually badLarge objects are expensive to allocate, Large objects are expensive to allocate, expensive to initialize and may fragment CMSexpensive to initialize and may fragment CMSNulling references is usually bad Nulling references is usually bad Avoid finalize()Avoid finalize()Avoid calling System.gc()Avoid calling System.gc()Avoid memory LeaksAvoid memory Leaks

Some sources: Objects in the wrong scopeSome sources: Objects in the wrong scope Instances of inner classesInstances of inner classes

Page 23: Taming Java Garbage Collector

Thank You!Thank You!

&&

Questions?Questions?