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
Ad

More Related Content

What's hot (20)

Going Beyond Microsoft IIS Short File Name Disclosure - NahamCon 2023 Edition
Going Beyond Microsoft IIS Short File Name Disclosure - NahamCon 2023 EditionGoing Beyond Microsoft IIS Short File Name Disclosure - NahamCon 2023 Edition
Going Beyond Microsoft IIS Short File Name Disclosure - NahamCon 2023 Edition
Soroush Dalili
 
How to Get Started with Cypress
How to Get Started with CypressHow to Get Started with Cypress
How to Get Started with Cypress
Applitools
 
How to go about testing in React?
How to go about testing in React? How to go about testing in React?
How to go about testing in React?
Lisa Gagarina
 
DEVOPS 에 대한 전반적인 소개 및 자동화툴 소개
DEVOPS 에 대한 전반적인 소개 및 자동화툴 소개DEVOPS 에 대한 전반적인 소개 및 자동화툴 소개
DEVOPS 에 대한 전반적인 소개 및 자동화툴 소개
태준 문
 
Test automation
Test automationTest automation
Test automation
Xavier Yin
 
Spring beans
Spring beansSpring beans
Spring beans
Roman Dovgan
 
Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...
Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...
Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...
Simplilearn
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
Mindfire Solutions
 
Jenkins tutorial
Jenkins tutorialJenkins tutorial
Jenkins tutorial
Mamun Rashid, CCDH
 
Async Programming in C# 5
Async Programming in C# 5Async Programming in C# 5
Async Programming in C# 5
Pratik Khasnabis
 
Cypress Automation
Cypress  AutomationCypress  Automation
Cypress Automation
Susantha Pathirana
 
Enterprise java unit-3_chapter-1-jsp
Enterprise  java unit-3_chapter-1-jspEnterprise  java unit-3_chapter-1-jsp
Enterprise java unit-3_chapter-1-jsp
sandeep54552
 
Asynchronous Programming in .NET
Asynchronous Programming in .NETAsynchronous Programming in .NET
Asynchronous Programming in .NET
Pierre-Luc Maheu
 
Handle Exceptions in Selenium Webdriver | Edureka
Handle Exceptions in Selenium Webdriver | EdurekaHandle Exceptions in Selenium Webdriver | Edureka
Handle Exceptions in Selenium Webdriver | Edureka
Edureka!
 
Java spring framework
Java spring frameworkJava spring framework
Java spring framework
Rajiv Gupta
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
priya_trivedi
 
온라인 게임에서 사례로 살펴보는 디버깅
온라인 게임에서 사례로 살펴보는 디버깅온라인 게임에서 사례로 살펴보는 디버깅
온라인 게임에서 사례로 살펴보는 디버깅
Ryan Park
 
NodeJS - Server Side JS
NodeJS - Server Side JS NodeJS - Server Side JS
NodeJS - Server Side JS
Ganesh Kondal
 
Ssl pinning and hsts header
Ssl pinning and hsts headerSsl pinning and hsts header
Ssl pinning and hsts header
Saleem M
 
Jenkins tutorial
Jenkins tutorialJenkins tutorial
Jenkins tutorial
HarikaReddy115
 
Going Beyond Microsoft IIS Short File Name Disclosure - NahamCon 2023 Edition
Going Beyond Microsoft IIS Short File Name Disclosure - NahamCon 2023 EditionGoing Beyond Microsoft IIS Short File Name Disclosure - NahamCon 2023 Edition
Going Beyond Microsoft IIS Short File Name Disclosure - NahamCon 2023 Edition
Soroush Dalili
 
How to Get Started with Cypress
How to Get Started with CypressHow to Get Started with Cypress
How to Get Started with Cypress
Applitools
 
How to go about testing in React?
How to go about testing in React? How to go about testing in React?
How to go about testing in React?
Lisa Gagarina
 
DEVOPS 에 대한 전반적인 소개 및 자동화툴 소개
DEVOPS 에 대한 전반적인 소개 및 자동화툴 소개DEVOPS 에 대한 전반적인 소개 및 자동화툴 소개
DEVOPS 에 대한 전반적인 소개 및 자동화툴 소개
태준 문
 
Test automation
Test automationTest automation
Test automation
Xavier Yin
 
Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...
Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...
Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...
Simplilearn
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
Mindfire Solutions
 
Enterprise java unit-3_chapter-1-jsp
Enterprise  java unit-3_chapter-1-jspEnterprise  java unit-3_chapter-1-jsp
Enterprise java unit-3_chapter-1-jsp
sandeep54552
 
Asynchronous Programming in .NET
Asynchronous Programming in .NETAsynchronous Programming in .NET
Asynchronous Programming in .NET
Pierre-Luc Maheu
 
Handle Exceptions in Selenium Webdriver | Edureka
Handle Exceptions in Selenium Webdriver | EdurekaHandle Exceptions in Selenium Webdriver | Edureka
Handle Exceptions in Selenium Webdriver | Edureka
Edureka!
 
Java spring framework
Java spring frameworkJava spring framework
Java spring framework
Rajiv Gupta
 
온라인 게임에서 사례로 살펴보는 디버깅
온라인 게임에서 사례로 살펴보는 디버깅온라인 게임에서 사례로 살펴보는 디버깅
온라인 게임에서 사례로 살펴보는 디버깅
Ryan Park
 
NodeJS - Server Side JS
NodeJS - Server Side JS NodeJS - Server Side JS
NodeJS - Server Side JS
Ganesh Kondal
 
Ssl pinning and hsts header
Ssl pinning and hsts headerSsl pinning and hsts header
Ssl pinning and hsts header
Saleem M
 

Viewers also liked (20)

NeoLoad Public Training 4.1
NeoLoad Public Training 4.1NeoLoad Public Training 4.1
NeoLoad Public Training 4.1
Harish Srivastava
 
如何更好地设计测试用例-BQConf
如何更好地设计测试用例-BQConf如何更好地设计测试用例-BQConf
如何更好地设计测试用例-BQConf
诸葛修车网-诸葛商城
 
Building a lock profiler on the JVM
Building a lock profiler on the JVMBuilding a lock profiler on the JVM
Building a lock profiler on the JVM
Pierre Laporte
 
Lock Interface in Java
Lock Interface in JavaLock Interface in Java
Lock Interface in Java
Home
 
大型网站架构演变
大型网站架构演变大型网站架构演变
大型网站架构演变
xiaozhen1900
 
Git基础培训
Git基础培训Git基础培训
Git基础培训
诸葛修车网-诸葛商城
 
自己的JVM自己救: 解救 OOM 實務經驗談 (JCConf 2015)
自己的JVM自己救: 解救 OOM 實務經驗談  (JCConf 2015)自己的JVM自己救: 解救 OOM 實務經驗談  (JCConf 2015)
自己的JVM自己救: 解救 OOM 實務經驗談 (JCConf 2015)
Leon Chen
 
Nginx+tomcat https 配置
Nginx+tomcat  https 配置Nginx+tomcat  https 配置
Nginx+tomcat https 配置
诸葛修车网-诸葛商城
 
Recipe 黃佳伶 葉愛慧
Recipe 黃佳伶 葉愛慧Recipe 黃佳伶 葉愛慧
Recipe 黃佳伶 葉愛慧
10y2try
 
Java多线程技术
Java多线程技术Java多线程技术
Java多线程技术
诸葛修车网-诸葛商城
 
App开发过程的演变之路
App开发过程的演变之路App开发过程的演变之路
App开发过程的演变之路
诸葛修车网-诸葛商城
 
Save JVM by Yourself: Real War Experiences of OOM
Save JVM by Yourself: Real War Experiences of OOMSave JVM by Yourself: Real War Experiences of OOM
Save JVM by Yourself: Real War Experiences of OOM
Leon Chen
 
浅谈项目管理(诸葛B2B电商研发部版改)
浅谈项目管理(诸葛B2B电商研发部版改)浅谈项目管理(诸葛B2B电商研发部版改)
浅谈项目管理(诸葛B2B电商研发部版改)
诸葛修车网-诸葛商城
 
Load testing using_neoload by kc
Load testing using_neoload by kcLoad testing using_neoload by kc
Load testing using_neoload by kc
krishna chaitanya
 
Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成
zhongbing liu
 
Concurrency: Best Practices
Concurrency: Best PracticesConcurrency: Best Practices
Concurrency: Best Practices
IndicThreads
 
[Java concurrency]02.basic thread synchronization
[Java concurrency]02.basic thread synchronization[Java concurrency]02.basic thread synchronization
[Java concurrency]02.basic thread synchronization
xuehan zhu
 
JVM及其调优
JVM及其调优JVM及其调优
JVM及其调优
zhongbing liu
 
Java concurrency - Thread pools
Java concurrency - Thread poolsJava concurrency - Thread pools
Java concurrency - Thread pools
maksym220889
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展
Leon Chen
 
Building a lock profiler on the JVM
Building a lock profiler on the JVMBuilding a lock profiler on the JVM
Building a lock profiler on the JVM
Pierre Laporte
 
Lock Interface in Java
Lock Interface in JavaLock Interface in Java
Lock Interface in Java
Home
 
大型网站架构演变
大型网站架构演变大型网站架构演变
大型网站架构演变
xiaozhen1900
 
自己的JVM自己救: 解救 OOM 實務經驗談 (JCConf 2015)
自己的JVM自己救: 解救 OOM 實務經驗談  (JCConf 2015)自己的JVM自己救: 解救 OOM 實務經驗談  (JCConf 2015)
自己的JVM自己救: 解救 OOM 實務經驗談 (JCConf 2015)
Leon Chen
 
Recipe 黃佳伶 葉愛慧
Recipe 黃佳伶 葉愛慧Recipe 黃佳伶 葉愛慧
Recipe 黃佳伶 葉愛慧
10y2try
 
Save JVM by Yourself: Real War Experiences of OOM
Save JVM by Yourself: Real War Experiences of OOMSave JVM by Yourself: Real War Experiences of OOM
Save JVM by Yourself: Real War Experiences of OOM
Leon Chen
 
浅谈项目管理(诸葛B2B电商研发部版改)
浅谈项目管理(诸葛B2B电商研发部版改)浅谈项目管理(诸葛B2B电商研发部版改)
浅谈项目管理(诸葛B2B电商研发部版改)
诸葛修车网-诸葛商城
 
Load testing using_neoload by kc
Load testing using_neoload by kcLoad testing using_neoload by kc
Load testing using_neoload by kc
krishna chaitanya
 
Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成
zhongbing liu
 
Concurrency: Best Practices
Concurrency: Best PracticesConcurrency: Best Practices
Concurrency: Best Practices
IndicThreads
 
[Java concurrency]02.basic thread synchronization
[Java concurrency]02.basic thread synchronization[Java concurrency]02.basic thread synchronization
[Java concurrency]02.basic thread synchronization
xuehan zhu
 
Java concurrency - Thread pools
Java concurrency - Thread poolsJava concurrency - Thread pools
Java concurrency - Thread pools
maksym220889
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展
Leon Chen
 
Ad

Similar to Performance Tuning - Understanding Garbage Collection (20)

Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
jaganmohanreddyk
 
JVM Garbage Collection Tuning
JVM Garbage Collection TuningJVM Garbage Collection Tuning
JVM Garbage Collection Tuning
ihji
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
Haim Yadid
 
Java garbage collection & GC friendly coding
Java garbage collection  & GC friendly codingJava garbage collection  & GC friendly coding
Java garbage collection & GC friendly coding
Md Ayub Ali Sarker
 
JVM Magic
JVM MagicJVM Magic
JVM Magic
Baruch Sadogursky
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
Mudit Gupta
 
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in JavaAn Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
Abhishek Asthana
 
Low latency Java apps
Low latency Java appsLow latency Java apps
Low latency Java apps
Simon Ritter
 
Java gc and JVM optimization
Java gc  and JVM optimizationJava gc  and JVM optimization
Java gc and JVM optimization
Rajan Jethva
 
Jvm lecture
Jvm lectureJvm lecture
Jvm lecture
sdslnmd
 
ZGC-SnowOne.pdf
ZGC-SnowOne.pdfZGC-SnowOne.pdf
ZGC-SnowOne.pdf
Monica Beckwith
 
What you need to know about GC
What you need to know about GCWhat you need to know about GC
What you need to know about GC
Kelum Senanayake
 
Garbage Collection of Java VM
Garbage Collection of Java VMGarbage Collection of Java VM
Garbage Collection of Java VM
Yongqiang Li
 
[Jbcn 2016] Garbage Collectors WTF!?
[Jbcn 2016] Garbage Collectors WTF!?[Jbcn 2016] Garbage Collectors WTF!?
[Jbcn 2016] Garbage Collectors WTF!?
Alonso Torres
 
jvm.pptx
jvm.pptxjvm.pptx
jvm.pptx
senthilnathan662693
 
Java Garbage Collector and The Memory Model
Java Garbage Collector and The Memory ModelJava Garbage Collector and The Memory Model
Java Garbage Collector and The Memory Model
Ernesto Arroyo Ron
 
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4
Amin Mesbahi
 
Java Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
Java Garbage Collectors – Moving to Java7 Garbage First (G1) CollectorJava Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
Java Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
Gurpreet Sachdeva
 
Diagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraDiagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - Cassandra
Jon Haddad
 
JVM memory metrics and rules for detecting possible OOM caused crash
JVM memory metrics and rules for detecting possible OOM caused crashJVM memory metrics and rules for detecting possible OOM caused crash
JVM memory metrics and rules for detecting possible OOM caused crash
Atharva Bhingarkar
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
jaganmohanreddyk
 
JVM Garbage Collection Tuning
JVM Garbage Collection TuningJVM Garbage Collection Tuning
JVM Garbage Collection Tuning
ihji
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
Haim Yadid
 
Java garbage collection & GC friendly coding
Java garbage collection  & GC friendly codingJava garbage collection  & GC friendly coding
Java garbage collection & GC friendly coding
Md Ayub Ali Sarker
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
Mudit Gupta
 
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in JavaAn Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
Abhishek Asthana
 
Low latency Java apps
Low latency Java appsLow latency Java apps
Low latency Java apps
Simon Ritter
 
Java gc and JVM optimization
Java gc  and JVM optimizationJava gc  and JVM optimization
Java gc and JVM optimization
Rajan Jethva
 
Jvm lecture
Jvm lectureJvm lecture
Jvm lecture
sdslnmd
 
What you need to know about GC
What you need to know about GCWhat you need to know about GC
What you need to know about GC
Kelum Senanayake
 
Garbage Collection of Java VM
Garbage Collection of Java VMGarbage Collection of Java VM
Garbage Collection of Java VM
Yongqiang Li
 
[Jbcn 2016] Garbage Collectors WTF!?
[Jbcn 2016] Garbage Collectors WTF!?[Jbcn 2016] Garbage Collectors WTF!?
[Jbcn 2016] Garbage Collectors WTF!?
Alonso Torres
 
Java Garbage Collector and The Memory Model
Java Garbage Collector and The Memory ModelJava Garbage Collector and The Memory Model
Java Garbage Collector and The Memory Model
Ernesto Arroyo Ron
 
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4
Amin Mesbahi
 
Java Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
Java Garbage Collectors – Moving to Java7 Garbage First (G1) CollectorJava Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
Java Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
Gurpreet Sachdeva
 
Diagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraDiagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - Cassandra
Jon Haddad
 
JVM memory metrics and rules for detecting possible OOM caused crash
JVM memory metrics and rules for detecting possible OOM caused crashJVM memory metrics and rules for detecting possible OOM caused crash
JVM memory metrics and rules for detecting possible OOM caused crash
Atharva Bhingarkar
 
Ad

Recently uploaded (20)

How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 

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