SlideShare a Scribd company logo
Understanding
Garbage Collection
(aka Automatic Memory Management)
Douglas Q. Hawkins
http://www.dougqh.net
dougqh@gmail.com
Why?
Leaky Abstraction
2 of 3 Optimization Flags Are For
Memory Management
but not about
tuning per se
...
...
HotSpot Collectors
Young
Old
-XX:+UseSerialGC
-XX:+UseParNewGC -XX:+UseParallelGC
-XX:+UseConcMarkSweepGC -XX:+UseParallelOldGC
Serial
Parallel
Scavenge
Serial Old
(Mark Compact)
Concurrent
Mark &
Sweep
Parallel
Old
(Mark Compact)
Parallel
Topics
Basic Garbage Collection Strategies & Terminology
Garbage Collection in HotSpot
Garbage Collection in J9 and JRockit
Alternatives to Garbage Collection
Azul C4
Fundamentals
Tasks for a Tracing GC
Identify Live Objects
Remove Dead Objects
Optionally, Coalesce Free Space
(Compaction)
Stack Frames
Mark & Sweep
Static Variables
System.in
System.out
Mark Compact
Allocation
4
8
Copy Collector
Tracing Problem
Random Walk of Heap
Cache Hostile
Bad Idea on Large Heap
Generational HypothesisBytesSurviving
Bytes Allocated
Infants
Generational
Young
Old / Tenured
Stack Frames
Static Variables
System.in
System.out
Remembered Set
Young
Old
Stack Frames
Static Variables
System.in
System.out
GC in
Java VMs
HotSpot Garbage Collector
Young Generation
Old Generation
Eden
S1S0
Young vs Tenured CollectionsBytesSurviving
Bytes Allocated
Young Tenured
Which Tracing Strategy?
Low
(Young Region)
Copy
High
(Tenured Region)
Mark & Sweep
OR
Mark Compact
Liveness Strategy
Object Allocation
Young Generation
Old Generation
Eden
S1S0
TLAB
Minor Collection
Young Generation
Old Generation
Eden
S1S0
TLAB
1 2
Major Collection
Young Generation
Old Generation
Eden
S1S0
Permanent Generation
Young Generation
Old Generation
Eden
S1S0
Permanent Generation
Bootstrap App Server Web Application
C C C C C C C C
Demo
https://visualvm.dev.java.net/plugins.htmlhttps://visualvm.dev.java.net/
Tuning
Newer JVMs can adaptively tune,
but you may not want to rely on that.
Steady State
8x
Newer JVMs can automatically tune,
but you may not want to rely on that.
-XX:MaxGCPauseMillis
-XX:GCTimeRatio
Meets these goals by adjusting the heap size.
Adaptive Tuning
Maximize Collection during Minor GCs
Avoid Major GCs at much as possible
Promote as little as possible
Maximize Available Memory
Adjust for 2 of 3 GC Goals
Throughput
Latency
Footprint
Basic Principles
Stops region resizing
which would require a full GC
Set Xms = Xmxinitial heap size max heap size
3-4x
Young (Xmn): 1-1.5x
Old: 2-3x
Set XX:PermSize =
XX:MaxPermSize
Again, stops region resizing
which would require a full GC
1.2-1.5x
GC Choices
Serial ParallelVS.
Stop the
World Concurrent
Compacting Non-compacting
VS.
Full VS. Incremental
Application Thread GC Thread
ParallelSerial
Stop the World!
Why Stop the World?
0x0C
0x0C
0x0C
0x04
0x04
0x04
BJL
Parallel Mark
Compact
Application Thread GC Thread
Concurrent
Mark & Sweep
Concurrent
Marking
Remark
Concurrent
Sweep
HotSpot Collectors
Young
Old
-XX:+UseSerialGC
-XX:+UseParNewGC -XX:+UseParallelGC
-XX:+UseConcMarkSweepGC -XX:+UseParallelOldGC
Serial
Parallel
Scavenge
Serial Old
(Mark Compact)
Concurrent
Mark &
Sweep
Parallel
Old
(Mark Compact)
Parallel
Application Thread GC Thread
Throughput vs Latency
If Latency is Too High,
Switch to ConcMarkSweep
CMS kicks in when old generation is 65% full.
Increase Old Generation an additional 25-30%
When compaction is needed it is serial,
so avoid compaction at all costs!
GCs in Other
JVMs
J9
Nursery
Tenured
SurvivorAllocate
C
JRockit
Nursery
Tenured
Keep Area Survivor
C C
C
Hybrid Mark Sweep & Copy
To From
New GCs in
Java VMs
G1:Garbage First & Balanced
G1 & Balanced GC
Unused Young Old
G1 - Minor Collection
Unused Young Old
Unused Young Old
G1 - Major Collection
Azul C4
Continuously Concurrent Compacting Collector
To From
http://www.infoq.com/articles/azul_gc_in_detail
Alternatives to Hard
References & GC
WeakReference
WR
WeakReference<Foo> fooRef =
new WeakReference<Foo>(foo);
Foo foo = fooRef.get();
SoftReference
SR
SoftReference<Foo> fooRef =
new SoftReference<Foo>(foo);
Foo foo = fooRef.get();
Garbage Collected if Low on Space
Be Careful With References
Reference<User> ref = ...
if ( ref.get() != null ) {
	 System.out.println(ref.get().getName());
}
Possible NullPointerException
Reference<User> ref = ...
User user = ref.get();
if ( user != null ) {
	 System.out.println(user.getName());
}
ConcurrentMap<Key, Graph> graphs = new MapMaker()
.concurrencyLevel(4)
.weakKeys()
.maximumSize(10000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.makeComputingMap(
new Function<Key, Graph>() {
public Graph apply(Key key) {
return createExpensiveGraph(key);
}
});
Guava MapMaker & CacheBuilder
PhantomReference
ReferenceQueue
PR
X
Direct ByteBuffers
ByteBuffer buffer = ByteBuffer.allocateDirect(...);
Additional Reading
The Garbage Collection Handbook
By Richard Jones, Antony Hoskin, Eliot Moss
http://www.amazon.com/The-Garbage-Collection-Handbook-Management/
Memory Management in the
HotSpot Java Virtual Machine
http://java.sun.com/j2se/reference/whitepapers/memorymanagement_whitepaper.pdf
By Charlie Hunt and Binu John
Java Performance
http://www.amazon.com/Java-Performance-Charlie-Hunt/
http://www.infoq.com/presentations/JVM-Performance-Tuning-twitter
By Attila Szegedi
Everything I Ever Learned About
JVM Performance Tuning
Questions?
Thanks, please fill out your evaluations!
Douglas Q. Hawkins
http://www.dougqh.net
dougqh@gmail.com

More Related Content

Similar to Understanding Garbage Collection Using Automatic Memory Management

Whippet: A new production embeddable garbage collector for Guile
Whippet: A new production embeddable garbage collector for GuileWhippet: A new production embeddable garbage collector for Guile
Whippet: A new production embeddable garbage collector for Guile
Igalia
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
jaganmohanreddyk
 

Similar to Understanding Garbage Collection Using Automatic Memory Management (20)

Choosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
Choosing Right Garbage Collector to Increase Efficiency of Java Memory UsageChoosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
Choosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
 
(JVM) Garbage Collection - Brown Bag Session
(JVM) Garbage Collection - Brown Bag Session(JVM) Garbage Collection - Brown Bag Session
(JVM) Garbage Collection - Brown Bag Session
 
JVM Magic
JVM MagicJVM Magic
JVM Magic
 
[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe
 
7 jvm-arguments-Confoo
7 jvm-arguments-Confoo7 jvm-arguments-Confoo
7 jvm-arguments-Confoo
 
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
 
JVM memory management & Diagnostics
JVM memory management & DiagnosticsJVM memory management & Diagnostics
JVM memory management & Diagnostics
 
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
 
Low pause GC in HotSpot
Low pause GC in HotSpotLow pause GC in HotSpot
Low pause GC in HotSpot
 
Whippet: A new production embeddable garbage collector for Guile
Whippet: A new production embeddable garbage collector for GuileWhippet: A new production embeddable garbage collector for Guile
Whippet: A new production embeddable garbage collector for Guile
 
Garbage collection in JVM
Garbage collection in JVMGarbage collection in JVM
Garbage collection in JVM
 
Performance tuning jvm
Performance tuning jvmPerformance tuning jvm
Performance tuning jvm
 
Java8 bench gc
Java8 bench gcJava8 bench gc
Java8 bench gc
 
Jvm Architecture
Jvm ArchitectureJvm Architecture
Jvm Architecture
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
 
Z Garbage Collector
Z Garbage CollectorZ Garbage Collector
Z Garbage Collector
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
 
List intersection for web search: Algorithms, Cost Models, and Optimizations
List intersection for web search: Algorithms, Cost Models, and OptimizationsList intersection for web search: Algorithms, Cost Models, and Optimizations
List intersection for web search: Algorithms, Cost Models, and Optimizations
 
Garbage First & You
Garbage First & YouGarbage First & You
Garbage First & You
 
Garbage First and you
Garbage First and youGarbage First and you
Garbage First and you
 

Recently uploaded

Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
 

Understanding Garbage Collection Using Automatic Memory Management