SlideShare a Scribd company logo
What you need to know about
GC
Java SE 6 HotSpot™ VM
K e l u m S e n a n a y a k e
High Level Agenda
• Introduction
• Generations
• Collectors
• Other Considerations
Introduction
Java is everywhere…
• Java™ Platform, Standard Edition (Java SE™) is
used for a wide variety of applications.
• JVM provides multiple garbage collectors,
each designed to satisfy different
requirements
Why memory?
• One strength of the J2SE platform is that it
shields the developer from the complexity of
memory allocation and garbage collection.
• However, once garbage collection is the
principal bottleneck, it is worth understanding
some aspects of this hidden implementation.
• When does the choice of a garbage collector
matter?
– For some applications, the answer is never
• Application can perform well in the presence of
garbage collection with pauses of modest frequency
and duration
– However, this is not the case for a large class of
applications
Generations
Garbage ?
Garbage Collection… How?
• The most straightforward garbage collection
algorithms simply iterate over every reachable
object.
– The time this approach takes is proportional to
the number of live objects
• “Weak generational hypothesis”
– “most objects survive for only a short period of
time”
Weak generational hypothesis
• Memory is managed in Generations
• Memory pools holding objects of different
ages
JVM Heap Layout
JVM Heap Layout
JVM Heap Layout
JVM Heap Layout
Performance Considerations
• Throughput
– is the percentage of total time not spent in
garbage collection, considered over long periods
of time
• Pauses
– are the times when an application appears
unresponsive because garbage collection is
occurring.
• A web server vs an interactive graphics
program
Performance…
• Footprint
– is the working set of a process, measured in pages and
cache lines
• Promptness
– is the time between when an object becomes dead
and when the memory becomes available
• In general, a particular generation sizing chooses
a trade-off between these considerations.
Performance…
• A very large young generation
– maximize throughput
– at the expense of footprint, promptness and
pause times.
• A small young generation
– young generation pauses can be minimized
– at the expense of throughput
Measurement
• -verbose:gc
information about the heap and garbage collection
to be printed at each collection.
[GC 325407K->83000K(776768K), 0.2300771 secs]
[GC 325816K->83372K(776768K), 0.2454258 secs]
[Full GC 267628K->83769K(776768K), 1.8479984 secs]
Measurement…
• -XX:+PrintGCDetails
[GC [DefNew: 64575K->959K(64576K), 0.0457646 secs]
196016K->133633K(261184K), 0.0459067 secs]
• -XX:+PrintGCTimeStamps
111.042: [GC 111.042: [DefNew: 8128K->8128K(8128K),
0.0000505 secs]111.042: [Tenured: 18154K->2311K(24576K),
0.1290354 secs] 26282K->2311K(32704K), 0.1293306 secs]
Sizing the Generations
• -Xms : initial sizes of the heap
• -Xmx : maximum sizes of the heap
• Collections occur when generations fill up
– Throughput is inversely proportional to the
amount of memory available
Sizing…
• By default, the virtual machine grows or
shrinks the heap at each collection to try to
keep the proportion of free space to live
objects at each collection within a specific
range
• -XX:MinHeapFreeRatio=<minimum>
• -XX:MaxHeapFreeRatio=<maximum>
Sizing…
MinHeapFreeRatio 40
MaxHeapFreeRatio 70
-Xms 3670k
-Xmx 64m
If the percent of free space in a generation falls below 40%, the generation will be
expanded to maintain 40% free space, up to the maximum allowed size of the
generation.
If the free space exceeds 70%, the generation will be contracted so that only 70% of
the space is free, subject to the minimum size of the generation.
The Young Generation
• The second most influential knob is the
proportion of the heap dedicated to the young
generation.
– The bigger the young generation, the less often
minor collections occur.
– Larger young generation implies a smaller tenured
generation (for a bounded heap size)
– will increase the frequency of major collections
• -XX:NewRatio : Defines the ratio between
the young and tenured generation
ex: -XX:NewRatio=3
• You can even decide the initial and max sizes
of the young space with parameters
NewSize and MaxNewSize
Survivor Space Sizing
• SurvivorRatio : can be used to tune the
size of the survivor spaces
• Sets the ratio between eden and a survivor
space
• This is often not as important for performance
• -XX:SurvivorRatio=6
• If survivor spaces are too small, copying
collection overflows directly into the tenured
generation.
• If survivor spaces are too large, they will be
uselessly empty.
Available Collectors
The serial collector
• Uses a single thread to perform all garbage
collection work
– No communication overhead between threads
– Best-suited to single processor machines
– Multiprocessors for applications with small data
sets (up to approximately 100MB)
• -XX:+UseSerialGC
The parallel collector
• a.k.a throughput collector
• Performs minor collections in parallel
• Medium- to large-sized data sets that are run
on multiprocessor or multi-threaded
hardware.
• -XX:+UseParallelGC
• -XX:+UseParallelOldGC
Parallel collector…
• Select the parallel collector if,
– Peak application performance is the first priority
– There are no pause time requirements or pauses
of one second or longer are acceptable
• On a machine with N processors the parallel
collector uses N garbage collector threads
• -XX:ParallelGCThreads=<N>
• -XX:MaxGCPauseMillis=<N>
– a hint that pause times of <N> milliseconds or less
are desired
• -XX:GCTimeRatio=<N>
– Sets the ratio of garbage collection time to
application time to 1 / (1 + <N>)
– -XX:GCTimeRatio=19 sets a goal of 1/20 or
5% of the total time in garbage collection.
Generation Size Adjustments
• Growing and shrinking are done at different rates
• By default a generation grows in increments of
20% and shrinks in increments of 5%.
-XX:YoungGenerationSizeIncrement=<Y> for the
young generation
-XX:TenuredGenerationSizeIncrement=<T> for the
tenured generation
-XX:AdaptiveSizeDecrementScaleFactor=<D>
If the growth increment is X percent, the decrement for
shrinking is X / D percent
The Concurrent Collector
• Select concurrent collector If
– response time is more important than overall
throughput
– garbage collection pauses must be kept shorter than
approximately one second
• Suitable for
– applications which have a relatively large set of long-
lived data (a large tenured generation)
– run on machines with two or more processors
• -XX:+UseConcMarkSweepGC
Steps of Concurrent Collection
1. Stop all application threads and identify the set of objects
reachable from roots, then resume all application threads
2. Concurrently trace the reachable object graph, using one or more
processors, while the application threads are executing
3. Concurrently retrace sections of the object graph that were
modified since the tracing in the previous step, using one
processor
4. Stop all application threads and retrace sections of the roots and
object graph that may have been modified since they were last
examined, then resume all application threads
5. Concurrently sweep up the unreachable objects to the free lists
used for allocation, using one processor
6. Concurrently resize the heap and prepare the support data
structures for the next collection cycle, using one processor
Starting a Concurrent Collection Cycle
• Serial collector
– A major collection occurs whenever the tenured
generation becomes full
– all application threads are stopped while the
collection is done.
– stop-the-world condition
• concurrent collector
– starts at a time such that the collection can finish
before the tenured generation becomes full
Starting…
• Concurrent collector maintains dynamic
estimates based on recent history
• A concurrent collection will also start if the
occupancy of the tenured generation exceeds
an initiating occupancy
• -XX:CMSInitiatingOccupancyFraction=<N>
– <N> is an integral percentage (0-100) of the
tenured generation size.
Incremental Mode
• Normally, the concurrent collector uses one or
more processors during the entire concurrent
tracing phase, without voluntarily releasing them.
• Also, one processor is used for the entire
concurrent sweep phase, again without releasing
it.
• Incremental mode solves this problem by
breaking up the concurrent phases into short
bursts of activity, which are scheduled to occur
mid-way between minor pauses.
[GC [1 CMS-initial-mark: 13991K(20288K)] 14103K(22400K), 0.0023781 secs]
[GC [DefNew: 2112K->64K(2112K), 0.0837052 secs] 16103K->15476K(22400K), 0.0838519
secs]
...
[GC [DefNew: 2077K->63K(2112K), 0.0126205 secs] 17552K->15855K(22400K), 0.0127482
secs]
[CMS-concurrent-mark: 0.267/0.374 secs]
[GC [DefNew: 2111K->64K(2112K), 0.0190851 secs] 17903K->16154K(22400K), 0.0191903
secs]
[CMS-concurrent-preclean: 0.044/0.064 secs]
[GC [1 CMS-remark: 16090K(20288K)] 17242K(22400K), 0.0210460 secs]
[GC [DefNew: 2112K->63K(2112K), 0.0716116 secs] 18177K->17382K(22400K), 0.0718204
secs]
[GC [DefNew: 2111K->63K(2112K), 0.0830392 secs] 19363K->18757K(22400K), 0.0832943
secs]
...
[GC [DefNew: 2111K->0K(2112K), 0.0035190 secs] 17527K->15479K(22400K), 0.0036052 secs]
[CMS-concurrent-sweep: 0.291/0.662 secs]
[GC [DefNew: 2048K->0K(2112K), 0.0013347 secs] 17527K->15479K(27912K), 0.0014231 secs]
[CMS-concurrent-reset: 0.016/0.016 secs]
[GC [DefNew: 2048K->1K(2112K), 0.0013936 secs] 17527K->15479K(27912K), 0.0014814 secs]
Incremental Mode…
• Recommended Options
– To use i-cms in Java SE 6, use the following
command line options:
-XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode
Excessive GC Time and OutOfMemoryError
• Parallel collector and concurrent collectore will
throw an OutOfMemoryError if too much time is
being spent in garbage collection
– If more than 98% of the total time is spent in garbage
collection and less than 2% of the heap is recovered
– Prevents applications from running for an extended
period of time while making little or no progress
• -XX:-UseGCOverheadLimit
– To disable this feature
Other Considerations
Permanent Generation Size
• The permanent generation does not have a
noticeable impact on GC performance
• However, some applications dynamically
generate and load many classes; for example,
some implementations of JavaServer Pages
(JSP) pages.
• -XX:MaxPermSize=<N>
The size of the JVM thread stack
• When Java creates a new thread, it pre-
allocates a fixed-size block of memory for that
thread's stack.
• If you have lots of threads - the memory
saving is the reduction in stack size times the
number of threads.
– May increase the chance of a Stack Overflow error
• On x86 Solaris/Linux it is 320k in the 32-bit
VM and 1024k in the 64-bit VM
JVM thread stack…
• (total RAM used by JVM process) = (heap size)
+ (number of threads)x(thread stack size)
• If application has a lot of threads might be
running out of memory
• You can reduce your stack size by running with
the -Xss option
• Ex : -Xss64k
References
[1] "Java SE 6 HotSpot™ Virtual Machine Garbage
Collection Tuning", [Online]. Available:
http://www.oracle.com/technetwork/java/javase/g
c-tuning-6-140523.html [Accessed 12-May-2014]
[2] "Frequently Asked Questions About the Java
HotSpot VM", [Online]. Available:
http://www.oracle.com/technetwork/java/hotspotf
aq-138619.htm [Accessed 12-May-2014]
Q & A

More Related Content

PDF
(JVM) Garbage Collection - Brown Bag Session
Jens Hadlich
 
PDF
Buzz Words Dunning Real-Time Learning
MapR Technologies
 
PPTX
Dealing with an Upside Down Internet With High Performance Time Series Database
DataWorks Summit
 
PPTX
Boston hug
Ted Dunning
 
PPTX
Storm 2012-03-29
Ted Dunning
 
PDF
Chronix Poster for the Poster Session FAST 2017
Florian Lautenschlager
 
PPT
HPTS talk on micro-sharding with Katta
Ted Dunning
 
PDF
AWS RDS Benchmark - CMG Brasil 2012
Rodrigo Campos
 
(JVM) Garbage Collection - Brown Bag Session
Jens Hadlich
 
Buzz Words Dunning Real-Time Learning
MapR Technologies
 
Dealing with an Upside Down Internet With High Performance Time Series Database
DataWorks Summit
 
Boston hug
Ted Dunning
 
Storm 2012-03-29
Ted Dunning
 
Chronix Poster for the Poster Session FAST 2017
Florian Lautenschlager
 
HPTS talk on micro-sharding with Katta
Ted Dunning
 
AWS RDS Benchmark - CMG Brasil 2012
Rodrigo Campos
 

What's hot (12)

PDF
A Fast and Efficient Time Series Storage Based on Apache Solr
QAware GmbH
 
PPTX
Exactly once with spark streaming
Quentin Ambard
 
PPTX
Jvm & Garbage collection tuning for low latencies application
Quentin Ambard
 
PPTX
“Show Me the Garbage!”, Understanding Garbage Collection
Haim Yadid
 
PDF
Efficient and Fast Time Series Storage - The missing link in dynamic software...
Florian Lautenschlager
 
PPTX
The Power of Both Choices: Practical Load Balancing for Distributed Stream Pr...
Anis Nasir
 
PPTX
Aerospike & GCE (LSPE Talk)
Sayyaparaju Sunil
 
PPTX
Predictive Maintenance with Deep Learning and Apache Flink
Dongwon Kim
 
PDF
Introduction VAUUM, Freezing, XID wraparound
Masahiko Sawada
 
PDF
DSD-INT 2017 High Performance Parallel Computing with iMODFLOW-MetaSWAP - Ver...
Deltares
 
PDF
Apache Gearpump next-gen streaming engine
Tianlun Zhang
 
PPT
CRAMM: Virtual Memory Support for Garbage-Collected Applications
Emery Berger
 
A Fast and Efficient Time Series Storage Based on Apache Solr
QAware GmbH
 
Exactly once with spark streaming
Quentin Ambard
 
Jvm & Garbage collection tuning for low latencies application
Quentin Ambard
 
“Show Me the Garbage!”, Understanding Garbage Collection
Haim Yadid
 
Efficient and Fast Time Series Storage - The missing link in dynamic software...
Florian Lautenschlager
 
The Power of Both Choices: Practical Load Balancing for Distributed Stream Pr...
Anis Nasir
 
Aerospike & GCE (LSPE Talk)
Sayyaparaju Sunil
 
Predictive Maintenance with Deep Learning and Apache Flink
Dongwon Kim
 
Introduction VAUUM, Freezing, XID wraparound
Masahiko Sawada
 
DSD-INT 2017 High Performance Parallel Computing with iMODFLOW-MetaSWAP - Ver...
Deltares
 
Apache Gearpump next-gen streaming engine
Tianlun Zhang
 
CRAMM: Virtual Memory Support for Garbage-Collected Applications
Emery Berger
 
Ad

Viewers also liked (20)

PDF
Couchbase - Yet Another Introduction
Kelum Senanayake
 
PDF
Node.js Introduction
Kelum Senanayake
 
PDF
Knight's Tour
Kelum Senanayake
 
PDF
Security Risks & Vulnerabilities in Skype
Kelum Senanayake
 
PDF
EJB 3.0 - Yet Another Introduction
Kelum Senanayake
 
PDF
Java GC - Pause tuning
ekino
 
PDF
Java gc
Niit
 
PDF
[BGOUG] Java GC - Friend or Foe
SAP HANA Cloud Platform
 
PPTX
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Anna Shymchenko
 
PPT
Java Garbage Collection(GC)- Study
Dhanu Gupta
 
PPTX
Java concurrency
Scheidt & Bachmann
 
PDF
Java Memory Model
Łukasz Koniecki
 
PDF
Java Memory Model
Skills Matter
 
PPTX
Николай Папирный Тема: "Java memory model для простых смертных"
Ciklum Minsk
 
ODP
Java Memory Consistency Model - concepts and context
Tomek Borek
 
PPTX
Java gc and JVM optimization
Rajan Jethva
 
ODP
Java memory model
Michał Warecki
 
PPTX
The Java Memory Model
CA Technologies
 
ODP
Java GC, Off-heap workshop
Valerii Moisieienko
 
PDF
How long can you afford to Stop The World?
Java Usergroup Berlin-Brandenburg
 
Couchbase - Yet Another Introduction
Kelum Senanayake
 
Node.js Introduction
Kelum Senanayake
 
Knight's Tour
Kelum Senanayake
 
Security Risks & Vulnerabilities in Skype
Kelum Senanayake
 
EJB 3.0 - Yet Another Introduction
Kelum Senanayake
 
Java GC - Pause tuning
ekino
 
Java gc
Niit
 
[BGOUG] Java GC - Friend or Foe
SAP HANA Cloud Platform
 
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Anna Shymchenko
 
Java Garbage Collection(GC)- Study
Dhanu Gupta
 
Java concurrency
Scheidt & Bachmann
 
Java Memory Model
Łukasz Koniecki
 
Java Memory Model
Skills Matter
 
Николай Папирный Тема: "Java memory model для простых смертных"
Ciklum Minsk
 
Java Memory Consistency Model - concepts and context
Tomek Borek
 
Java gc and JVM optimization
Rajan Jethva
 
Java memory model
Michał Warecki
 
The Java Memory Model
CA Technologies
 
Java GC, Off-heap workshop
Valerii Moisieienko
 
How long can you afford to Stop The World?
Java Usergroup Berlin-Brandenburg
 
Ad

Similar to What you need to know about GC (20)

PDF
JVM Garbage Collection Tuning
ihji
 
PPTX
Memory Management in the Java Virtual Machine(Garbage collection)
Prashanth Kumar
 
ODP
Garbage Collection in Hotspot JVM
jaganmohanreddyk
 
PDF
Performance Tuning - Understanding Garbage Collection
Haribabu Nandyal Padmanaban
 
PDF
Let's talk about Garbage Collection
Haim Yadid
 
PDF
Tuning IBMs Generational GC
Chris Bailey
 
PPTX
JVM Magic
Baruch Sadogursky
 
ODP
Garbage collection
Mudit Gupta
 
PPTX
G1 Garbage Collector - Big Heaps and Low Pauses?
C2B2 Consulting
 
PDF
Low latency Java apps
Simon Ritter
 
PPT
«Большие объёмы данных и сборка мусора в Java
Olga Lavrentieva
 
PPTX
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Anna Shymchenko
 
PDF
Six Myths and Paradoxes of Garbage Collection
Holly Cummins
 
PPTX
java memory management & gc
exsuns
 
PPT
Fun421 stephens
Tess Ferrandez
 
PDF
JVM and Garbage Collection Tuning
Kai Koenig
 
PDF
Java at Scale, Dallas JUG, October 2013
Azul Systems Inc.
 
PPTX
Jvm lecture
sdslnmd
 
PPTX
JVM memory management & Diagnostics
Dhaval Shah
 
PDF
Basics of JVM Tuning
Vladislav Gangan
 
JVM Garbage Collection Tuning
ihji
 
Memory Management in the Java Virtual Machine(Garbage collection)
Prashanth Kumar
 
Garbage Collection in Hotspot JVM
jaganmohanreddyk
 
Performance Tuning - Understanding Garbage Collection
Haribabu Nandyal Padmanaban
 
Let's talk about Garbage Collection
Haim Yadid
 
Tuning IBMs Generational GC
Chris Bailey
 
Garbage collection
Mudit Gupta
 
G1 Garbage Collector - Big Heaps and Low Pauses?
C2B2 Consulting
 
Low latency Java apps
Simon Ritter
 
«Большие объёмы данных и сборка мусора в Java
Olga Lavrentieva
 
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Anna Shymchenko
 
Six Myths and Paradoxes of Garbage Collection
Holly Cummins
 
java memory management & gc
exsuns
 
Fun421 stephens
Tess Ferrandez
 
JVM and Garbage Collection Tuning
Kai Koenig
 
Java at Scale, Dallas JUG, October 2013
Azul Systems Inc.
 
Jvm lecture
sdslnmd
 
JVM memory management & Diagnostics
Dhaval Shah
 
Basics of JVM Tuning
Vladislav Gangan
 

Recently uploaded (20)

PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PDF
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PDF
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
PDF
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
PDF
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PDF
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
Presentation about variables and constant.pptx
kr2589474
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 

What you need to know about GC

  • 1. What you need to know about GC Java SE 6 HotSpot™ VM K e l u m S e n a n a y a k e
  • 2. High Level Agenda • Introduction • Generations • Collectors • Other Considerations
  • 4. Java is everywhere… • Java™ Platform, Standard Edition (Java SE™) is used for a wide variety of applications. • JVM provides multiple garbage collectors, each designed to satisfy different requirements
  • 5. Why memory? • One strength of the J2SE platform is that it shields the developer from the complexity of memory allocation and garbage collection. • However, once garbage collection is the principal bottleneck, it is worth understanding some aspects of this hidden implementation.
  • 6. • When does the choice of a garbage collector matter? – For some applications, the answer is never • Application can perform well in the presence of garbage collection with pauses of modest frequency and duration – However, this is not the case for a large class of applications
  • 9. Garbage Collection… How? • The most straightforward garbage collection algorithms simply iterate over every reachable object. – The time this approach takes is proportional to the number of live objects • “Weak generational hypothesis” – “most objects survive for only a short period of time”
  • 11. • Memory is managed in Generations • Memory pools holding objects of different ages
  • 16. Performance Considerations • Throughput – is the percentage of total time not spent in garbage collection, considered over long periods of time • Pauses – are the times when an application appears unresponsive because garbage collection is occurring. • A web server vs an interactive graphics program
  • 17. Performance… • Footprint – is the working set of a process, measured in pages and cache lines • Promptness – is the time between when an object becomes dead and when the memory becomes available • In general, a particular generation sizing chooses a trade-off between these considerations.
  • 18. Performance… • A very large young generation – maximize throughput – at the expense of footprint, promptness and pause times. • A small young generation – young generation pauses can be minimized – at the expense of throughput
  • 19. Measurement • -verbose:gc information about the heap and garbage collection to be printed at each collection. [GC 325407K->83000K(776768K), 0.2300771 secs] [GC 325816K->83372K(776768K), 0.2454258 secs] [Full GC 267628K->83769K(776768K), 1.8479984 secs]
  • 20. Measurement… • -XX:+PrintGCDetails [GC [DefNew: 64575K->959K(64576K), 0.0457646 secs] 196016K->133633K(261184K), 0.0459067 secs] • -XX:+PrintGCTimeStamps 111.042: [GC 111.042: [DefNew: 8128K->8128K(8128K), 0.0000505 secs]111.042: [Tenured: 18154K->2311K(24576K), 0.1290354 secs] 26282K->2311K(32704K), 0.1293306 secs]
  • 21. Sizing the Generations • -Xms : initial sizes of the heap • -Xmx : maximum sizes of the heap • Collections occur when generations fill up – Throughput is inversely proportional to the amount of memory available
  • 22. Sizing… • By default, the virtual machine grows or shrinks the heap at each collection to try to keep the proportion of free space to live objects at each collection within a specific range • -XX:MinHeapFreeRatio=<minimum> • -XX:MaxHeapFreeRatio=<maximum>
  • 23. Sizing… MinHeapFreeRatio 40 MaxHeapFreeRatio 70 -Xms 3670k -Xmx 64m If the percent of free space in a generation falls below 40%, the generation will be expanded to maintain 40% free space, up to the maximum allowed size of the generation. If the free space exceeds 70%, the generation will be contracted so that only 70% of the space is free, subject to the minimum size of the generation.
  • 24. The Young Generation • The second most influential knob is the proportion of the heap dedicated to the young generation. – The bigger the young generation, the less often minor collections occur. – Larger young generation implies a smaller tenured generation (for a bounded heap size) – will increase the frequency of major collections
  • 25. • -XX:NewRatio : Defines the ratio between the young and tenured generation ex: -XX:NewRatio=3 • You can even decide the initial and max sizes of the young space with parameters NewSize and MaxNewSize
  • 26. Survivor Space Sizing • SurvivorRatio : can be used to tune the size of the survivor spaces • Sets the ratio between eden and a survivor space • This is often not as important for performance • -XX:SurvivorRatio=6
  • 27. • If survivor spaces are too small, copying collection overflows directly into the tenured generation. • If survivor spaces are too large, they will be uselessly empty.
  • 29. The serial collector • Uses a single thread to perform all garbage collection work – No communication overhead between threads – Best-suited to single processor machines – Multiprocessors for applications with small data sets (up to approximately 100MB) • -XX:+UseSerialGC
  • 30. The parallel collector • a.k.a throughput collector • Performs minor collections in parallel • Medium- to large-sized data sets that are run on multiprocessor or multi-threaded hardware. • -XX:+UseParallelGC • -XX:+UseParallelOldGC
  • 31. Parallel collector… • Select the parallel collector if, – Peak application performance is the first priority – There are no pause time requirements or pauses of one second or longer are acceptable • On a machine with N processors the parallel collector uses N garbage collector threads • -XX:ParallelGCThreads=<N>
  • 32. • -XX:MaxGCPauseMillis=<N> – a hint that pause times of <N> milliseconds or less are desired • -XX:GCTimeRatio=<N> – Sets the ratio of garbage collection time to application time to 1 / (1 + <N>) – -XX:GCTimeRatio=19 sets a goal of 1/20 or 5% of the total time in garbage collection.
  • 33. Generation Size Adjustments • Growing and shrinking are done at different rates • By default a generation grows in increments of 20% and shrinks in increments of 5%. -XX:YoungGenerationSizeIncrement=<Y> for the young generation -XX:TenuredGenerationSizeIncrement=<T> for the tenured generation -XX:AdaptiveSizeDecrementScaleFactor=<D> If the growth increment is X percent, the decrement for shrinking is X / D percent
  • 34. The Concurrent Collector • Select concurrent collector If – response time is more important than overall throughput – garbage collection pauses must be kept shorter than approximately one second • Suitable for – applications which have a relatively large set of long- lived data (a large tenured generation) – run on machines with two or more processors • -XX:+UseConcMarkSweepGC
  • 35. Steps of Concurrent Collection 1. Stop all application threads and identify the set of objects reachable from roots, then resume all application threads 2. Concurrently trace the reachable object graph, using one or more processors, while the application threads are executing 3. Concurrently retrace sections of the object graph that were modified since the tracing in the previous step, using one processor 4. Stop all application threads and retrace sections of the roots and object graph that may have been modified since they were last examined, then resume all application threads 5. Concurrently sweep up the unreachable objects to the free lists used for allocation, using one processor 6. Concurrently resize the heap and prepare the support data structures for the next collection cycle, using one processor
  • 36. Starting a Concurrent Collection Cycle • Serial collector – A major collection occurs whenever the tenured generation becomes full – all application threads are stopped while the collection is done. – stop-the-world condition • concurrent collector – starts at a time such that the collection can finish before the tenured generation becomes full
  • 37. Starting… • Concurrent collector maintains dynamic estimates based on recent history • A concurrent collection will also start if the occupancy of the tenured generation exceeds an initiating occupancy • -XX:CMSInitiatingOccupancyFraction=<N> – <N> is an integral percentage (0-100) of the tenured generation size.
  • 38. Incremental Mode • Normally, the concurrent collector uses one or more processors during the entire concurrent tracing phase, without voluntarily releasing them. • Also, one processor is used for the entire concurrent sweep phase, again without releasing it. • Incremental mode solves this problem by breaking up the concurrent phases into short bursts of activity, which are scheduled to occur mid-way between minor pauses.
  • 39. [GC [1 CMS-initial-mark: 13991K(20288K)] 14103K(22400K), 0.0023781 secs] [GC [DefNew: 2112K->64K(2112K), 0.0837052 secs] 16103K->15476K(22400K), 0.0838519 secs] ... [GC [DefNew: 2077K->63K(2112K), 0.0126205 secs] 17552K->15855K(22400K), 0.0127482 secs] [CMS-concurrent-mark: 0.267/0.374 secs] [GC [DefNew: 2111K->64K(2112K), 0.0190851 secs] 17903K->16154K(22400K), 0.0191903 secs] [CMS-concurrent-preclean: 0.044/0.064 secs] [GC [1 CMS-remark: 16090K(20288K)] 17242K(22400K), 0.0210460 secs] [GC [DefNew: 2112K->63K(2112K), 0.0716116 secs] 18177K->17382K(22400K), 0.0718204 secs] [GC [DefNew: 2111K->63K(2112K), 0.0830392 secs] 19363K->18757K(22400K), 0.0832943 secs] ... [GC [DefNew: 2111K->0K(2112K), 0.0035190 secs] 17527K->15479K(22400K), 0.0036052 secs] [CMS-concurrent-sweep: 0.291/0.662 secs] [GC [DefNew: 2048K->0K(2112K), 0.0013347 secs] 17527K->15479K(27912K), 0.0014231 secs] [CMS-concurrent-reset: 0.016/0.016 secs] [GC [DefNew: 2048K->1K(2112K), 0.0013936 secs] 17527K->15479K(27912K), 0.0014814 secs]
  • 40. Incremental Mode… • Recommended Options – To use i-cms in Java SE 6, use the following command line options: -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode
  • 41. Excessive GC Time and OutOfMemoryError • Parallel collector and concurrent collectore will throw an OutOfMemoryError if too much time is being spent in garbage collection – If more than 98% of the total time is spent in garbage collection and less than 2% of the heap is recovered – Prevents applications from running for an extended period of time while making little or no progress • -XX:-UseGCOverheadLimit – To disable this feature
  • 43. Permanent Generation Size • The permanent generation does not have a noticeable impact on GC performance • However, some applications dynamically generate and load many classes; for example, some implementations of JavaServer Pages (JSP) pages. • -XX:MaxPermSize=<N>
  • 44. The size of the JVM thread stack • When Java creates a new thread, it pre- allocates a fixed-size block of memory for that thread's stack. • If you have lots of threads - the memory saving is the reduction in stack size times the number of threads. – May increase the chance of a Stack Overflow error • On x86 Solaris/Linux it is 320k in the 32-bit VM and 1024k in the 64-bit VM
  • 45. JVM thread stack… • (total RAM used by JVM process) = (heap size) + (number of threads)x(thread stack size) • If application has a lot of threads might be running out of memory • You can reduce your stack size by running with the -Xss option • Ex : -Xss64k
  • 46. References [1] "Java SE 6 HotSpot™ Virtual Machine Garbage Collection Tuning", [Online]. Available: http://www.oracle.com/technetwork/java/javase/g c-tuning-6-140523.html [Accessed 12-May-2014] [2] "Frequently Asked Questions About the Java HotSpot VM", [Online]. Available: http://www.oracle.com/technetwork/java/hotspotf aq-138619.htm [Accessed 12-May-2014]
  • 47. Q & A