SlideShare a Scribd company logo
8
Most read
14
Most read
22
Most read
Garbage Collection
In computer science, garbage collection (GC) is a
form of Automatic Memory Management.
The garbage collector attempts to reclaim the
memory occupied by objects that are no longer in
use by the program.
Garbage collection was invented by John McCarthy
around 1959 to abstract away manual memory
management in Lisp.
Static vs Dynamic memory allocation
Creating an array of ten integers in C (static)
int array[10];
Creating an array of integers in C (dynamic)
int * array = malloc(N * sizeof(int));
free(array);
In C++
Foo* fooPtr = new Foo()
delete fooPtr
Languages without GC
C, C++*, D*, Objective-C*,
Rust*
Languages
Languages wit GC
Java, Go, PHP, Python, Scala*,
Haskell, ….
Garbage Collection Algorithms
Reference Counting Mark and Sweep
Copy Collection
Generational Collection
Reference Counting
Keep an extra integer (“reference count”) to every
heap-allocated data structure.
● With a new reference ++refCount.
● When a reference disappears --refCount.
● If refCount == 0, then reclaim the storage
+ Easy to implement
+ Real Time cleanup
- Additional storage
- Speed: incr, decr
- Cycles not cleaned
C++, Objective-C, Rust,
PHP, Python….
Tracing Collectors vs Reference Counting
Mark and Sweep
Mark Phase: traverses all objects,
starting with roots, and marks every
object found as alive.
Sweep Phase: traverses all
objects, reclaim the storage of
unmarked objects.
+ Handles cycles
+ Easy to implement
- Stops the world**
- Scans the entire heap
Copy Collection
+ Handles cycles
+ Automatic Compaction
- Stops the world**
- Changes addresses
- 2nd half is unused
Generational Collection
● Most objects die young
● Newer objects usually point to older objects
+ Same as Copy (unidirectional)
+ Less Objects to Copy
+ Frequency of collection
+ Faster
- Stops the world**
- Complex to implement
Java Stack vs Heap
Pre and Post Java 8 Heap
Java Garbage Collectors
● Serial Collector - -XX:+UseSerialGC
● Parallel Collector - -XX:+UseParallelGC
● Concurrent Mark & Sweep Collector - -XX:+UseConcMarkSweepGC
● Garbage First (G1) Collector - -XX:+UseG1GC
Serial GC
● Stops the world
● Uses single thread
● Designed for single CPU small Heap apps
● Do not use it!
● Young Gen - Copy Collector
● Old Gen - Mark and Sweep
Parallel GC (The Default Collector)
● Stops the world
● Designed to work with multiple CPUs
● Uses multiple threads
● Expect high latencies when GC runs (-XX:MaxGCPauseMillis=<N>)
● Young Gen - Copy Collector
● Old Gen - Mark and Sweep
Concurrent Mark and Sweep (CMS) GC
● Stops the world (relatively short pauses)
● Designed to work with multiple CPUs
● Uses multiple threads
● Good for low latency apps
● Young Gen - Copy Collector
● Old Gen - Concurrent Mark-Sweep
Garbage First (G1) Collector
● Heap is split into (typically 2048) smaller regions
● Avoids collecting the entire heap at once, instead collects incrementally
○ The regions that contain the most garbage are collected first
● Soft real-time garbage collector (Predictable | Configurable STW)
● Compaction is relatively easy
● Uses multiple threads and is good for > 6G heap sizes. Java 9 Default GC.
● Young Gen - Copy Collector
● Old Gen - Concurrent Mark-Sweep
Minor GC vs Major GC vs Full GC
Minor GC cleans the Young Generation
Major GC cleans the Old Generation
Full GC cleans the Young and Old Generation
AdProxy Latency Issues
JVM Options to know
java -XX:+PrintFlagsFinal -version | grep HeapSize
uintx InitialHeapSize := 268435456 {product}
uintx MaxHeapSize := 4294967296 {product}
java -Xms256m -Xmx2048m
-XX:MaxGCPauseMillis=200
-XX:+PrintGCDetails -XX:+PrintGCDateStamps
Java Memory Leak
Hashmap keys without proper equals and hashcode
Maps or lists which are growing forever
Troubleshooting
UI Options: jconsole or jvisualvm.
Command Line:
jstat -gc -t processID 1s
jmap -heap processID
jmap -dump:live,format=b,file=heap.bin processID
jhat heap.bin
JVM Options: -XX:+PrintGCDetails-XX:+PrintGCDateStamps
Thank you!

More Related Content

PPTX
Garbage collection
PDF
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
PPTX
Biopure case solution
PPT
Type Checking(Compiler Design) #ShareThisIfYouLike
PDF
Linked list implementation of Queue
PPTX
Breadth First Search & Depth First Search
PDF
ASANA ppt
PPTX
Golden Apple Fast Food Restaurant EIF FEASIBILITY REPORT
Garbage collection
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Biopure case solution
Type Checking(Compiler Design) #ShareThisIfYouLike
Linked list implementation of Queue
Breadth First Search & Depth First Search
ASANA ppt
Golden Apple Fast Food Restaurant EIF FEASIBILITY REPORT

What's hot (20)

PPTX
Code generation
PPTX
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
PPTX
Stressen's matrix multiplication
PPTX
Associative memory and set associative memory mapping
PPTX
MAT Chapter 1
PPTX
Memory Organization
PPTX
Structure of the compiler
PPT
Memory management
ODP
Garbage collection
PPT
Paging.ppt
PPTX
memory hierarchy
PPTX
Instance based learning
PPTX
Two-way Deterministic Finite Automata
PPTX
Multiprocessor Architecture (Advanced computer architecture)
PPTX
8 queens problem using back tracking
PPT
Np cooks theorem
PPTX
Paging and Segmentation in Operating System
PPTX
Basic Computer Organization and Design
PPS
Cache memory
PPTX
Heap Management
Code generation
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
Stressen's matrix multiplication
Associative memory and set associative memory mapping
MAT Chapter 1
Memory Organization
Structure of the compiler
Memory management
Garbage collection
Paging.ppt
memory hierarchy
Instance based learning
Two-way Deterministic Finite Automata
Multiprocessor Architecture (Advanced computer architecture)
8 queens problem using back tracking
Np cooks theorem
Paging and Segmentation in Operating System
Basic Computer Organization and Design
Cache memory
Heap Management
Ad

Similar to Introduction to Garbage Collection (20)

PDF
(JVM) Garbage Collection - Brown Bag Session
PDF
Choosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
PDF
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
PDF
[BGOUG] Java GC - Friend or Foe
PPTX
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
PPTX
G1 Garbage Collector - Big Heaps and Low Pauses?
PDF
Garbage First and You!
PDF
Garbage First and you
PDF
Garbage First & You
PDF
2013 05 ny
PPTX
Jvm Architecture
PDF
Let's talk about Garbage Collection
PPTX
Gpu workshop cluster universe: scripting cuda
PPTX
How I Sped up Complex Matrix-Vector Multiplication: Finding Intel MKL's "S
PDF
JVM Performance Tuning
PDF
Elastic JVM for Scalable Java EE Applications Running in Containers #Jakart...
PPTX
An introduction to G1 collector for busy developers
PDF
Large-Scale, Semi-Automated Go Garbage Collection Tuning at Uber
PDF
Programar para GPUs
PPTX
CodeStock - Exploring .NET memory management - a trip down memory lane
(JVM) Garbage Collection - Brown Bag Session
Choosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
“Show Me the Garbage!”, Garbage Collection a Friend or a Foe
[BGOUG] Java GC - Friend or Foe
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
G1 Garbage Collector - Big Heaps and Low Pauses?
Garbage First and You!
Garbage First and you
Garbage First & You
2013 05 ny
Jvm Architecture
Let's talk about Garbage Collection
Gpu workshop cluster universe: scripting cuda
How I Sped up Complex Matrix-Vector Multiplication: Finding Intel MKL's "S
JVM Performance Tuning
Elastic JVM for Scalable Java EE Applications Running in Containers #Jakart...
An introduction to G1 collector for busy developers
Large-Scale, Semi-Automated Go Garbage Collection Tuning at Uber
Programar para GPUs
CodeStock - Exploring .NET memory management - a trip down memory lane
Ad

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
System and Network Administration Chapter 2
PDF
top salesforce developer skills in 2025.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Nekopoi APK 2025 free lastest update
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
System and Network Administraation Chapter 3
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Understanding Forklifts - TECH EHS Solution
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Wondershare Filmora 15 Crack With Activation Key [2025
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
System and Network Administration Chapter 2
top salesforce developer skills in 2025.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Design an Analysis of Algorithms I-SECS-1021-03
Nekopoi APK 2025 free lastest update
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
wealthsignaloriginal-com-DS-text-... (1).pdf
System and Network Administraation Chapter 3
2025 Textile ERP Trends: SAP, Odoo & Oracle
Operating system designcfffgfgggggggvggggggggg
Understanding Forklifts - TECH EHS Solution
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Reimagine Home Health with the Power of Agentic AI​
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Upgrade and Innovation Strategies for SAP ERP Customers
Lecture 3: Operating Systems Introduction to Computer Hardware Systems

Introduction to Garbage Collection

  • 2. In computer science, garbage collection (GC) is a form of Automatic Memory Management. The garbage collector attempts to reclaim the memory occupied by objects that are no longer in use by the program. Garbage collection was invented by John McCarthy around 1959 to abstract away manual memory management in Lisp.
  • 3. Static vs Dynamic memory allocation Creating an array of ten integers in C (static) int array[10]; Creating an array of integers in C (dynamic) int * array = malloc(N * sizeof(int)); free(array); In C++ Foo* fooPtr = new Foo() delete fooPtr
  • 4. Languages without GC C, C++*, D*, Objective-C*, Rust* Languages Languages wit GC Java, Go, PHP, Python, Scala*, Haskell, ….
  • 5. Garbage Collection Algorithms Reference Counting Mark and Sweep Copy Collection Generational Collection
  • 6. Reference Counting Keep an extra integer (“reference count”) to every heap-allocated data structure. ● With a new reference ++refCount. ● When a reference disappears --refCount. ● If refCount == 0, then reclaim the storage + Easy to implement + Real Time cleanup - Additional storage - Speed: incr, decr - Cycles not cleaned C++, Objective-C, Rust, PHP, Python….
  • 7. Tracing Collectors vs Reference Counting
  • 8. Mark and Sweep Mark Phase: traverses all objects, starting with roots, and marks every object found as alive. Sweep Phase: traverses all objects, reclaim the storage of unmarked objects. + Handles cycles + Easy to implement - Stops the world** - Scans the entire heap
  • 9. Copy Collection + Handles cycles + Automatic Compaction - Stops the world** - Changes addresses - 2nd half is unused
  • 10. Generational Collection ● Most objects die young ● Newer objects usually point to older objects + Same as Copy (unidirectional) + Less Objects to Copy + Frequency of collection + Faster - Stops the world** - Complex to implement
  • 12. Pre and Post Java 8 Heap
  • 13. Java Garbage Collectors ● Serial Collector - -XX:+UseSerialGC ● Parallel Collector - -XX:+UseParallelGC ● Concurrent Mark & Sweep Collector - -XX:+UseConcMarkSweepGC ● Garbage First (G1) Collector - -XX:+UseG1GC
  • 14. Serial GC ● Stops the world ● Uses single thread ● Designed for single CPU small Heap apps ● Do not use it! ● Young Gen - Copy Collector ● Old Gen - Mark and Sweep
  • 15. Parallel GC (The Default Collector) ● Stops the world ● Designed to work with multiple CPUs ● Uses multiple threads ● Expect high latencies when GC runs (-XX:MaxGCPauseMillis=<N>) ● Young Gen - Copy Collector ● Old Gen - Mark and Sweep
  • 16. Concurrent Mark and Sweep (CMS) GC ● Stops the world (relatively short pauses) ● Designed to work with multiple CPUs ● Uses multiple threads ● Good for low latency apps ● Young Gen - Copy Collector ● Old Gen - Concurrent Mark-Sweep
  • 17. Garbage First (G1) Collector ● Heap is split into (typically 2048) smaller regions ● Avoids collecting the entire heap at once, instead collects incrementally ○ The regions that contain the most garbage are collected first ● Soft real-time garbage collector (Predictable | Configurable STW) ● Compaction is relatively easy ● Uses multiple threads and is good for > 6G heap sizes. Java 9 Default GC. ● Young Gen - Copy Collector ● Old Gen - Concurrent Mark-Sweep
  • 18. Minor GC vs Major GC vs Full GC Minor GC cleans the Young Generation Major GC cleans the Old Generation Full GC cleans the Young and Old Generation
  • 20. JVM Options to know java -XX:+PrintFlagsFinal -version | grep HeapSize uintx InitialHeapSize := 268435456 {product} uintx MaxHeapSize := 4294967296 {product} java -Xms256m -Xmx2048m -XX:MaxGCPauseMillis=200 -XX:+PrintGCDetails -XX:+PrintGCDateStamps
  • 21. Java Memory Leak Hashmap keys without proper equals and hashcode Maps or lists which are growing forever
  • 22. Troubleshooting UI Options: jconsole or jvisualvm. Command Line: jstat -gc -t processID 1s jmap -heap processID jmap -dump:live,format=b,file=heap.bin processID jhat heap.bin JVM Options: -XX:+PrintGCDetails-XX:+PrintGCDateStamps