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

What's hot

TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
Denis Bazhin
 
Automation Testing & TDD
Automation Testing & TDDAutomation Testing & TDD
Automation Testing & TDD
Nhật Nguyễn Khắc
 

What's hot (20)

Selenium
SeleniumSelenium
Selenium
 
Selenium
SeleniumSelenium
Selenium
 
Java troubleshooting thread dump
Java troubleshooting thread dumpJava troubleshooting thread dump
Java troubleshooting thread dump
 
Automation - web testing with selenium
Automation - web testing with seleniumAutomation - web testing with selenium
Automation - web testing with selenium
 
Introduction to jest
Introduction to jestIntroduction to jest
Introduction to jest
 
Cucumber ppt
Cucumber pptCucumber ppt
Cucumber ppt
 
Cucumber & gherkin language
Cucumber & gherkin languageCucumber & gherkin language
Cucumber & gherkin language
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
 
TDD and Unit Testing in Golang
TDD and Unit Testing in GolangTDD and Unit Testing in Golang
TDD and Unit Testing in Golang
 
QSpiders - Automation using Selenium
QSpiders - Automation using SeleniumQSpiders - Automation using Selenium
QSpiders - Automation using Selenium
 
JavaScript Interview Questions and Answers | Full Stack Web Development Train...
JavaScript Interview Questions and Answers | Full Stack Web Development Train...JavaScript Interview Questions and Answers | Full Stack Web Development Train...
JavaScript Interview Questions and Answers | Full Stack Web Development Train...
 
TestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaTestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | Edureka
 
Automation Testing & TDD
Automation Testing & TDDAutomation Testing & TDD
Automation Testing & TDD
 
Testcontainers - Geekout EE 2017 presentation
Testcontainers - Geekout EE 2017 presentationTestcontainers - Geekout EE 2017 presentation
Testcontainers - Geekout EE 2017 presentation
 
Web Services and Introduction of SOAPUI
Web Services and Introduction of SOAPUIWeb Services and Introduction of SOAPUI
Web Services and Introduction of SOAPUI
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation Guide
 
Introduction to Bdd and cucumber
Introduction to Bdd and cucumberIntroduction to Bdd and cucumber
Introduction to Bdd and cucumber
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)
 

Viewers also liked

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
 
Concurrency: Best Practices
Concurrency: Best PracticesConcurrency: Best Practices
Concurrency: Best Practices
IndicThreads
 

Viewers also liked (20)

Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
Performance Tuning -  Memory leaks, Thread deadlocks, JDK toolsPerformance Tuning -  Memory leaks, Thread deadlocks, JDK tools
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
 
NeoLoad Public Training 4.1
NeoLoad Public Training 4.1NeoLoad Public Training 4.1
NeoLoad Public Training 4.1
 
如何更好地设计测试用例-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
 
Lock Interface in Java
Lock Interface in JavaLock Interface in Java
Lock Interface in Java
 
大型网站架构演变
大型网站架构演变大型网站架构演变
大型网站架构演变
 
Git基础培训
Git基础培训Git基础培训
Git基础培训
 
自己的JVM自己救: 解救 OOM 實務經驗談 (JCConf 2015)
自己的JVM自己救: 解救 OOM 實務經驗談  (JCConf 2015)自己的JVM自己救: 解救 OOM 實務經驗談  (JCConf 2015)
自己的JVM自己救: 解救 OOM 實務經驗談 (JCConf 2015)
 
Nginx+tomcat https 配置
Nginx+tomcat  https 配置Nginx+tomcat  https 配置
Nginx+tomcat https 配置
 
Recipe 黃佳伶 葉愛慧
Recipe 黃佳伶 葉愛慧Recipe 黃佳伶 葉愛慧
Recipe 黃佳伶 葉愛慧
 
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
 
浅谈项目管理(诸葛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
 
Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成Thrift+scribe实现分布式日志收集,并与log4j集成
Thrift+scribe实现分布式日志收集,并与log4j集成
 
Concurrency: Best Practices
Concurrency: Best PracticesConcurrency: Best Practices
Concurrency: Best Practices
 
[Java concurrency]02.basic thread synchronization
[Java concurrency]02.basic thread synchronization[Java concurrency]02.basic thread synchronization
[Java concurrency]02.basic thread synchronization
 
JVM及其调优
JVM及其调优JVM及其调优
JVM及其调优
 
Java concurrency - Thread pools
Java concurrency - Thread poolsJava concurrency - Thread pools
Java concurrency - Thread pools
 

Similar to Performance Tuning - Understanding Garbage Collection

Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
jaganmohanreddyk
 
Jvm lecture
Jvm lectureJvm lecture
Jvm lecture
sdslnmd
 

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
 
JVM Garbage Collection Tuning
JVM Garbage Collection TuningJVM Garbage Collection Tuning
JVM Garbage Collection Tuning
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
 
Java garbage collection & GC friendly coding
Java garbage collection  & GC friendly codingJava garbage collection  & GC friendly coding
Java garbage collection & GC friendly coding
 
JVM Magic
JVM MagicJVM Magic
JVM Magic
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
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
 
Low latency Java apps
Low latency Java appsLow latency Java apps
Low latency Java apps
 
Java gc and JVM optimization
Java gc  and JVM optimizationJava gc  and JVM optimization
Java gc and JVM optimization
 
Jvm lecture
Jvm lectureJvm lecture
Jvm lecture
 
ZGC-SnowOne.pdf
ZGC-SnowOne.pdfZGC-SnowOne.pdf
ZGC-SnowOne.pdf
 
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
 
Garbage Collection of Java VM
Garbage Collection of Java VMGarbage Collection of Java VM
Garbage Collection of Java VM
 
[Jbcn 2016] Garbage Collectors WTF!?
[Jbcn 2016] Garbage Collectors WTF!?[Jbcn 2016] Garbage Collectors WTF!?
[Jbcn 2016] Garbage Collectors WTF!?
 
jvm.pptx
jvm.pptxjvm.pptx
jvm.pptx
 
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
 
.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
 
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
 
Diagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraDiagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - Cassandra
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
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)

Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.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...
 
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 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
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...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
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
 
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...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
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
 
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
 
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
 
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 ...
 
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...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 

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