SlideShare a Scribd company logo
Performance Tuning
Garbage Collection
Haribabu Nandyal
Performance Engineering Expert
Agenda - Garbage Collection
1. JVM Architecture
 Components of GC
2. Fundamentals of Garbage Collection
3. GC Algorithms
• Generational GC
 Young generation
 Old Generation
 Minor and Major GC
• Serial and Parallel GC
• STW and Concurrent GC
• CMS GC
• More time in GC means more application thread pauses.
• Higher the number of objects, higher is the memory foot print
and thereby more work for GC to reclaim memory.
• Large heap - more time for GC to trigger.
• Small heap - less time but frequent GCs.
Why GC Monitoring important?
• GC compute intensive - CPU overhead. More the time taken
by GC, slower will be your application.
• Throughput : Total time spent in not doing GC.
• Pause Time: The time for which the application threads
stopped while collecting.
• Promptness: time between objects death and its collection
HotSpot JVM: Architecture
1) Classloader:
Classloader is a subsystem of JVM that is used to load class files.
2) Class(Method) Area:
It stores per-class structures such as the runtime constant pool, field and
method data, the code for methods.
3) Heap:
It is the runtime data area in which objects are allocated.
4) Stack:
Each thread has its own PC register and a Java stack. A new frame is created
each time a method is invoked and is destroyed when its method invocation
completes. The stack stores primitive local variables and object references along
with the call stack.
5) Program Counter Register:
It contains the address of the Java virtual machine instruction currently being
executed.
HotSpot JVM: Architecture
6) Native Method Stack:
It contains all the native methods used in the application.
7) Execution Engine:
 A virtual processor
 Interpreter:
Reads bytecode stream and executes the instructions.
 Just-In-Time(JIT) compiler:
JIT compiles parts of the byte code that have similar functionality at the
same time, and hence reduces the amount of time needed to compile. Here the
term “compiler” refers to a translator from the instruction set of a JVM to the
instruction set of a specific CPU.
HotSpot JVM: Architecture
JVM Heap Structure
Before Marking
After Marking
Garbage Collector first performs a task called
Marking.
Each object which the GC meets is marked as
being used and will not be deleted in the
sweeping stage.
Fundamentals of Garbage
Collection
Fundamentals of Garbage
Collection
The Sweeping stage is where the deletion of the objects takes
place.
The traditional way is to let the allocator methods use complex data
structures to search the memory for the required space.
Fundamentals of Garbage Collection
Deletion with
compacting
Compact the memory by moving objects close to each other.
Object allocation is faster.
Fundamentals of Garbage Collection
Objects Lifetime
Generational Garbage
Collection
Generational Garbage Collection
HotSpot uses “Generational Collectors”
HotSpot Java heap is allocated into generational spaces.
Memory space is divided into three sections:
• Young Generation (for young objects)
 Eden
 A “from” survivor space
 A “to” survivor space
• Tenured (old) generation (for old objects)
• Permanent generation (meta data, classes and so on)
Features of Young Generational Space
• GCs occur relatively frequent.
• GCs are fast and efficient because young generation space is
usually small and likely to contain a lot of short lived objects.
• Objects that survive some number of young generation
collections are promoted to old generation heap space.
Features of Old Generational Space
• Typically larger than young generation heap space
• Its occupancy grows slowly
• GCs are infrequent but takes significantly longer time to
complete than young generational heap space.
 GCs in old generation space should be minimized.
Generational Garbage Collection
Generational Garbage Collection
New objects are allocated to the Eden space.
When Eden space is full, a minor GC is triggered
(Stop the world event)
Young GC process
Generational Garbage Collection
Unreferenced objects are garbage collected.
Referenced objects are copied to survivor space and have
their age incremented.
Young GC process
Generational Garbage Collection
After objects are moved to the survivor space, Eden space is
cleared.
The from survivor space is also cleared.
Young GC process
Generational Garbage Collection
Next Minor GC
Referenced objects from last GC become "from" Survivor
space.
Referenced objects are copied to the "to" survivor space.
Surviving objects ages are incremented.
Young GC process
Generational Garbage Collection
Young GC process
Generational Garbage Collection
Promoted to Old Space
When age threshold is reached , objects are eventually promoted to
tenured space.
Young GC process
Generational Garbage Collection
Process repeats at each minor GC
When objects reach an age threshold, they are copied to
old generation
Generational Garbage Collection
• Young Generation
 Eden
 Survivor Space
 Objects age here
• Minor garbage collections are always "Stop the
World" events
• Minor garbage collections can be
 Single-threaded
Serial GC
 Multithreaded (Parallel)
• Parallel GC
• Concurrent GC
• G1 GC
Young GC process - Summary
Improve performance of GC
For young generation (Minor GC)
threads
timegc
threads
Default GC Parallel GC
Young
Generation
Serial GC vs Parallel GC
Reduce pause time to collect Old Generation
For old generation (Full GC)
Enabled by -XX:+UseConcMarkSweepGC
threads
timegc
threads
STW GC Concurrent GC
Old
Generation
STW GC vs Concurrent GC
Serial Mark Sweep vs Concurrent
Mark Sweep (CMS)
• Eliminating dead object in “Eden” space.
• Moving live object from “Eden” to empty survival space (“To” space).
• Object that are too big, are copied directly to old space.
• Eliminating dead object in survival “From” space
• Mature objects are moved to old space
• Moving live object from used survival space (“From”) to empty survival
space (“To” space).
• Object that are too big, are copied directly to old space.
Serial Collector
Initial Mark
• Identifies root objects.
• Stop the world phase
Concurrent
Mark
• Marks live object that are reachable from the root object graph.
• Concurrent
Remark
• Revisits changed objects for liveliness check (Objects change
during the concurrent phases)
• Stop the world phase
Concurrent
Sweep
• All garbage objects are swept.
• Concurrent
Concurrent Mark-Sweep
• 32 bit Java processes heap size
 Varies according to the OS and platform
 determined by the process memory layout
 32bit architecture has an addressable range of: 2^32 is
4GB
• 64 bit processes do not have this limit
 Limit exists, but is so large it can be effectively ignored
 Addressability usually between 2^44 and 2^64 : 16+
TeraBytes
Maximum Possible Heap Size
A 32 bit Java process has a 4 GB memory which is shared
by the Java Heap, Native Heap and the Operating System.
GC will adapt heap size to keep occupancy between 40% and
70%
• Heap occupancy over 70% causes frequent GC cycles
 Which, In general means reduced performance
• Heap occupancy below 40% means infrequent GC cycles, but
cycles longer than they need to be
 Which means longer pause times than necessary
 Which generally means reduced performance (high latency)
• The maximum heap size setting should therefore be 43% larger
than the maximum occupancy of the application
 Maximum occupancy + 43% means occupancy at 70% of total
heap
Eg. For 70MB occupancy, 100MB heap size required (70MB + 43%
of 70MB)
“The Right” Java heap size
Fixed heap vs. Variable heap
Minimum heap size (-Xms) = Maximum heap size (-Xmx)?
• Variable Heap Sizes
 GC will adapt heap size to keep occupancy between 40% and
70% which expands and shrinks the Java heap
 Allows for scenarios where usage varies over time, where
variations would take usage outside of the 40-70% window
• Fixed Heap Sizes
 Does not expand or shrink the Java heap
Requirement Problem Solution
Fast and responsive Requires low pause GC CMS Collector
Needs to support a large
number of concurrent
users
• Generates a lot of
short lived objects
• Hardware support
Multi-threaded Hardware
Large Eden memory space
(1 GB)
Generates a large
number of proxy
classes
Requires Permanent
generation tuning
Large permanent generation
space (384 MB)
Holds large data
collections in memory
Requires a large old
memory space
Large Old memory space (4
GB)
Heap size is 5 GB 32 Bit JVM is not enough 64 Bit JVM and Hardware
GC Related Problems & Solutions
Process Flow to Tune JVM

More Related Content

PDF
Java Performance Tuning
PPTX
Write microservice in golang
PDF
Linux performance tuning & stabilization tips (mysqlconf2010)
PDF
Understanding jvm gc advanced
PDF
NDC12_Lockless게임서버설계와구현
PPTX
AWS로 게임 기반 다지기 - 김병수, 박진성 :: AWS Game Master 온라인 세미나 #3
PPTX
Introduction to microservices
PDF
Redis cluster
Java Performance Tuning
Write microservice in golang
Linux performance tuning & stabilization tips (mysqlconf2010)
Understanding jvm gc advanced
NDC12_Lockless게임서버설계와구현
AWS로 게임 기반 다지기 - 김병수, 박진성 :: AWS Game Master 온라인 세미나 #3
Introduction to microservices
Redis cluster

What's hot (20)

PDF
Optimizing {Java} Application Performance on Kubernetes
PDF
서버 아키텍쳐 입문
PDF
아마존 클라우드와 함께한 1개월, 쿠키런 사례중심 (KGC 2013)
PPTX
Scylla Summit 2022: Scylla 5.0 New Features, Part 1
PDF
실전 서버 부하테스트 노하우
PPTX
Scylla Summit 2022: Scylla 5.0 New Features, Part 2
PPTX
Saga about distributed business transactions in microservices world
PDF
서버 성능에 대한 정의와 이해
PPTX
Scylla Summit 2022: Making Schema Changes Safe with Raft
PDF
.NET에서 비동기 프로그래밍 배우기
PDF
Cassandra at Instagram 2016 (Dikang Gu, Facebook) | Cassandra Summit 2016
PDF
How the Postgres Query Optimizer Works
 
PDF
AWS를 활용하여 Daily Report 만들기 : 로그 수집부터 자동화된 분석까지
PDF
CQRS and event sourcing
PDF
Single page applications
ODP
Distributed Transactions: Saga Patterns
PDF
RoFormer: Enhanced Transformer with Rotary Position Embedding
PDF
AWS와 함께 한 쿠키런 서버 Re-architecting 사례 (Gaming on AWS)
PPTX
Microservice intro
PDF
How to build massive service for advance
Optimizing {Java} Application Performance on Kubernetes
서버 아키텍쳐 입문
아마존 클라우드와 함께한 1개월, 쿠키런 사례중심 (KGC 2013)
Scylla Summit 2022: Scylla 5.0 New Features, Part 1
실전 서버 부하테스트 노하우
Scylla Summit 2022: Scylla 5.0 New Features, Part 2
Saga about distributed business transactions in microservices world
서버 성능에 대한 정의와 이해
Scylla Summit 2022: Making Schema Changes Safe with Raft
.NET에서 비동기 프로그래밍 배우기
Cassandra at Instagram 2016 (Dikang Gu, Facebook) | Cassandra Summit 2016
How the Postgres Query Optimizer Works
 
AWS를 활용하여 Daily Report 만들기 : 로그 수집부터 자동화된 분석까지
CQRS and event sourcing
Single page applications
Distributed Transactions: Saga Patterns
RoFormer: Enhanced Transformer with Rotary Position Embedding
AWS와 함께 한 쿠키런 서버 Re-architecting 사례 (Gaming on AWS)
Microservice intro
How to build massive service for advance
Ad

Viewers also liked (20)

PDF
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
PDF
NeoLoad Public Training 4.1
PDF
如何更好地设计测试用例-BQConf
ODP
Building a lock profiler on the JVM
PPT
Lock Interface in Java
PDF
大型网站架构演变
PDF
PDF
自己的JVM自己救: 解救 OOM 實務經驗談 (JCConf 2015)
PDF
Nginx+tomcat https 配置
PPT
Recipe 黃佳伶 葉愛慧
PDF
Java多线程技术
PDF
App开发过程的演变之路
PDF
Save JVM by Yourself: Real War Experiences of OOM
PDF
浅谈项目管理(诸葛B2B电商研发部版改)
PPT
Load testing using_neoload by kc
PPTX
Thrift+scribe实现分布式日志收集,并与log4j集成
PDF
Concurrency: Best Practices
PDF
[Java concurrency]02.basic thread synchronization
PDF
JVM及其调优
PPTX
Java concurrency - Thread pools
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
NeoLoad Public Training 4.1
如何更好地设计测试用例-BQConf
Building a lock profiler on the JVM
Lock Interface in Java
大型网站架构演变
自己的JVM自己救: 解救 OOM 實務經驗談 (JCConf 2015)
Nginx+tomcat https 配置
Recipe 黃佳伶 葉愛慧
Java多线程技术
App开发过程的演变之路
Save JVM by Yourself: Real War Experiences of OOM
浅谈项目管理(诸葛B2B电商研发部版改)
Load testing using_neoload by kc
Thrift+scribe实现分布式日志收集,并与log4j集成
Concurrency: Best Practices
[Java concurrency]02.basic thread synchronization
JVM及其调优
Java concurrency - Thread pools
Ad

Similar to Performance Tuning - Understanding Garbage Collection (20)

ODP
Garbage Collection in Hotspot JVM
PDF
JVM Garbage Collection Tuning
PDF
Let's talk about Garbage Collection
PPTX
Java garbage collection & GC friendly coding
PPTX
ODP
Garbage collection
PPT
An Introduction to JVM Internals and Garbage Collection in Java
PDF
Low latency Java apps
PPTX
Java gc and JVM optimization
PPTX
Jvm lecture
PDF
ZGC-SnowOne.pdf
PDF
What you need to know about GC
PPTX
Garbage Collection of Java VM
PDF
[Jbcn 2016] Garbage Collectors WTF!?
PPTX
PDF
Java Garbage Collector and The Memory Model
PDF
.NET Core, ASP.NET Core Course, Session 4
PPT
Java Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
PDF
Diagnosing Problems in Production - Cassandra
PDF
淺談 Java GC 原理、調教和 新發展
Garbage Collection in Hotspot JVM
JVM Garbage Collection Tuning
Let's talk about Garbage Collection
Java garbage collection & GC friendly coding
Garbage collection
An Introduction to JVM Internals and Garbage Collection in Java
Low latency Java apps
Java gc and JVM optimization
Jvm lecture
ZGC-SnowOne.pdf
What you need to know about GC
Garbage Collection of Java VM
[Jbcn 2016] Garbage Collectors WTF!?
Java Garbage Collector and The Memory Model
.NET Core, ASP.NET Core Course, Session 4
Java Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
Diagnosing Problems in Production - Cassandra
淺談 Java GC 原理、調教和 新發展

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
August Patch Tuesday
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
cloud_computing_Infrastucture_as_cloud_p
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
MIND Revenue Release Quarter 2 2025 Press Release
TLE Review Electricity (Electricity).pptx
Network Security Unit 5.pdf for BCA BBA.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
August Patch Tuesday
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectroscopy.pptx food analysis technology
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Diabetes mellitus diagnosis method based random forest with bat algorithm
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
cloud_computing_Infrastucture_as_cloud_p
Group 1 Presentation -Planning and Decision Making .pptx
Univ-Connecticut-ChatGPT-Presentaion.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Digital-Transformation-Roadmap-for-Companies.pptx
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...

Performance Tuning - Understanding Garbage Collection

  • 1. Performance Tuning Garbage Collection Haribabu Nandyal Performance Engineering Expert
  • 2. Agenda - Garbage Collection 1. JVM Architecture  Components of GC 2. Fundamentals of Garbage Collection 3. GC Algorithms • Generational GC  Young generation  Old Generation  Minor and Major GC • Serial and Parallel GC • STW and Concurrent GC • CMS GC
  • 3. • More time in GC means more application thread pauses. • Higher the number of objects, higher is the memory foot print and thereby more work for GC to reclaim memory. • Large heap - more time for GC to trigger. • Small heap - less time but frequent GCs. Why GC Monitoring important? • GC compute intensive - CPU overhead. More the time taken by GC, slower will be your application. • Throughput : Total time spent in not doing GC. • Pause Time: The time for which the application threads stopped while collecting. • Promptness: time between objects death and its collection
  • 5. 1) Classloader: Classloader is a subsystem of JVM that is used to load class files. 2) Class(Method) Area: It stores per-class structures such as the runtime constant pool, field and method data, the code for methods. 3) Heap: It is the runtime data area in which objects are allocated. 4) Stack: Each thread has its own PC register and a Java stack. A new frame is created each time a method is invoked and is destroyed when its method invocation completes. The stack stores primitive local variables and object references along with the call stack. 5) Program Counter Register: It contains the address of the Java virtual machine instruction currently being executed. HotSpot JVM: Architecture
  • 6. 6) Native Method Stack: It contains all the native methods used in the application. 7) Execution Engine:  A virtual processor  Interpreter: Reads bytecode stream and executes the instructions.  Just-In-Time(JIT) compiler: JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed to compile. Here the term “compiler” refers to a translator from the instruction set of a JVM to the instruction set of a specific CPU. HotSpot JVM: Architecture
  • 8. Before Marking After Marking Garbage Collector first performs a task called Marking. Each object which the GC meets is marked as being used and will not be deleted in the sweeping stage. Fundamentals of Garbage Collection
  • 10. The Sweeping stage is where the deletion of the objects takes place. The traditional way is to let the allocator methods use complex data structures to search the memory for the required space. Fundamentals of Garbage Collection
  • 11. Deletion with compacting Compact the memory by moving objects close to each other. Object allocation is faster. Fundamentals of Garbage Collection
  • 14. Generational Garbage Collection HotSpot uses “Generational Collectors” HotSpot Java heap is allocated into generational spaces. Memory space is divided into three sections: • Young Generation (for young objects)  Eden  A “from” survivor space  A “to” survivor space • Tenured (old) generation (for old objects) • Permanent generation (meta data, classes and so on)
  • 15. Features of Young Generational Space • GCs occur relatively frequent. • GCs are fast and efficient because young generation space is usually small and likely to contain a lot of short lived objects. • Objects that survive some number of young generation collections are promoted to old generation heap space. Features of Old Generational Space • Typically larger than young generation heap space • Its occupancy grows slowly • GCs are infrequent but takes significantly longer time to complete than young generational heap space.  GCs in old generation space should be minimized. Generational Garbage Collection
  • 17. New objects are allocated to the Eden space. When Eden space is full, a minor GC is triggered (Stop the world event) Young GC process Generational Garbage Collection
  • 18. Unreferenced objects are garbage collected. Referenced objects are copied to survivor space and have their age incremented. Young GC process Generational Garbage Collection
  • 19. After objects are moved to the survivor space, Eden space is cleared. The from survivor space is also cleared. Young GC process Generational Garbage Collection
  • 20. Next Minor GC Referenced objects from last GC become "from" Survivor space. Referenced objects are copied to the "to" survivor space. Surviving objects ages are incremented. Young GC process Generational Garbage Collection
  • 21. Young GC process Generational Garbage Collection
  • 22. Promoted to Old Space When age threshold is reached , objects are eventually promoted to tenured space. Young GC process Generational Garbage Collection
  • 23. Process repeats at each minor GC When objects reach an age threshold, they are copied to old generation Generational Garbage Collection
  • 24. • Young Generation  Eden  Survivor Space  Objects age here • Minor garbage collections are always "Stop the World" events • Minor garbage collections can be  Single-threaded Serial GC  Multithreaded (Parallel) • Parallel GC • Concurrent GC • G1 GC Young GC process - Summary
  • 25. Improve performance of GC For young generation (Minor GC) threads timegc threads Default GC Parallel GC Young Generation Serial GC vs Parallel GC
  • 26. Reduce pause time to collect Old Generation For old generation (Full GC) Enabled by -XX:+UseConcMarkSweepGC threads timegc threads STW GC Concurrent GC Old Generation STW GC vs Concurrent GC
  • 27. Serial Mark Sweep vs Concurrent Mark Sweep (CMS)
  • 28. • Eliminating dead object in “Eden” space. • Moving live object from “Eden” to empty survival space (“To” space). • Object that are too big, are copied directly to old space. • Eliminating dead object in survival “From” space • Mature objects are moved to old space • Moving live object from used survival space (“From”) to empty survival space (“To” space). • Object that are too big, are copied directly to old space. Serial Collector
  • 29. Initial Mark • Identifies root objects. • Stop the world phase Concurrent Mark • Marks live object that are reachable from the root object graph. • Concurrent Remark • Revisits changed objects for liveliness check (Objects change during the concurrent phases) • Stop the world phase Concurrent Sweep • All garbage objects are swept. • Concurrent Concurrent Mark-Sweep
  • 30. • 32 bit Java processes heap size  Varies according to the OS and platform  determined by the process memory layout  32bit architecture has an addressable range of: 2^32 is 4GB • 64 bit processes do not have this limit  Limit exists, but is so large it can be effectively ignored  Addressability usually between 2^44 and 2^64 : 16+ TeraBytes Maximum Possible Heap Size A 32 bit Java process has a 4 GB memory which is shared by the Java Heap, Native Heap and the Operating System.
  • 31. GC will adapt heap size to keep occupancy between 40% and 70% • Heap occupancy over 70% causes frequent GC cycles  Which, In general means reduced performance • Heap occupancy below 40% means infrequent GC cycles, but cycles longer than they need to be  Which means longer pause times than necessary  Which generally means reduced performance (high latency) • The maximum heap size setting should therefore be 43% larger than the maximum occupancy of the application  Maximum occupancy + 43% means occupancy at 70% of total heap Eg. For 70MB occupancy, 100MB heap size required (70MB + 43% of 70MB) “The Right” Java heap size
  • 32. Fixed heap vs. Variable heap Minimum heap size (-Xms) = Maximum heap size (-Xmx)? • Variable Heap Sizes  GC will adapt heap size to keep occupancy between 40% and 70% which expands and shrinks the Java heap  Allows for scenarios where usage varies over time, where variations would take usage outside of the 40-70% window • Fixed Heap Sizes  Does not expand or shrink the Java heap
  • 33. Requirement Problem Solution Fast and responsive Requires low pause GC CMS Collector Needs to support a large number of concurrent users • Generates a lot of short lived objects • Hardware support Multi-threaded Hardware Large Eden memory space (1 GB) Generates a large number of proxy classes Requires Permanent generation tuning Large permanent generation space (384 MB) Holds large data collections in memory Requires a large old memory space Large Old memory space (4 GB) Heap size is 5 GB 32 Bit JVM is not enough 64 Bit JVM and Hardware GC Related Problems & Solutions
  • 34. Process Flow to Tune JVM