Taming Java Garbage Collector

Post on 10-May-2015

545 views 2 download

Tags:

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

Taming the Taming the Java Garbage Java Garbage

CollectorCollector

Daya Daya AtapattuAtapattu

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

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

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

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.

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

GC RegionsGC Regions

Young Generation CollectionYoung Generation Collection

Old Gen and CompactionOld Gen and Compaction

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

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

CMSCMS

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.

G1 CollectorG1 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

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]

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)

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.

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

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

Thank You!Thank You!

&&

Questions?Questions?