What is Throughput in
gc(garbage collection) in java ?
·
In short, Throughput is
the time not spent in garbage collection (GC) ( in percent).
·
Throughput focuses on
maximizing the amount of work by an application in a specific period of time.
Examples of how throughput might be measured include >
o
The number of
transactions completed in a given time.
o
The number of jobs that
a batch program can complete in an hour.
o The number of database queries that can be
completed in an hour.
What are pauses in
gc(garbage collection) in java ?
·
Pauses is applications
pauses i.e. when application doesn’t gives any response because of
garbage collection (GC).
JVM Heap memory (Hotspot heap structure) with
diagram in java:
JVM Heap memory (Hotspot heap structure) in
java consists of following elements>
1)
Young Generation
i)
Eden,
ii)
S0 (Survivor space 0)
iii)
S1 (Survivor space 1)
2)
Old Generation
(Tenured)
3)
Permanent Generation.
So, JVM Heap memory
(Hotspot heap structure) is divided into three parts Young Generation, Old
Generation (tenured) and Permanent Generation.
Young Generation is further divided into Eden, S0
(Survivor space 0) and S1 (Survivor space 1).
GARBAGE
COLLECTION (Minor
and major garbage collection) in JVM Heap memory (i.e. in young, old and
permanent generation)
3.1) Young Generation (Minor
garbage collection occurs in Young Generation)
·
New objects are
allocated in Young generation.
·
Minor
garbage collection occurs
in Young Generation.
When minor garbage
collection?
·
When the young
generation fills up, this causes a minor garbage collection.
·
All the unreferenced
(dead) objects are cleaned up from young generation.
When objects are moved
from young to old generation in JVM heap?
·
Some of the objects
which aren't cleaned up survive in young generation and gets aged.
Eventually such objects are moved from young to old generation.
What is Stop the
World Event?
·
Minor garbage
collections are called Stop the World events.
·
All
the non-daemon threads running in application are stopped during minor garbage
collections (i.e. the application
stops for while).
·
Daemon threads performs minor garbage collection. (Daemon threads are low priority threads which runs intermittently in background for
doing garbage collection).
Division of Young
Generation
a) Eden b) S0 (Survivor space 0) c) S1 (Survivor space 1)
3.2) Old Generation or (tenured generation) - (Major
garbage collection occurs in Old Generation)
·
The Old Generation
is used for storing the long surviving aged objects (Some of the objects which
aren't cleaned up survive in young generation and gets aged.
Eventually such objects are moved from young to old generation).
·
Major
garbage collection occurs
in Old Generation.
At what time (or what
age) objects are moved from young to old generation in JVM heap?
·
There is some threshold
set for young generation object and when that age is met, the object gets moved
to the old generation during gc(garbage collection) in java.
What is major garbage
collection in java?
·
When
the old generation fills up,
this causes a major garbage collection. Objects are cleaned up
from old generation.
·
Major collection is much
slower than minor garbage collection in jvm heap because it involves all
live objects.
Major garbage collection
are Stop the World Event in java?
·
Major garbage
collections are also called Stop the World events.
·
All the non-daemon
threads running in application are stopped during major garbage collections.
Major gc(garbage
collections) in responsive applications in java?
·
Major garbage
collections should be minimized for responsive applications because
applications must not be stopped for long.
Optimizing Major gc(garbage collections) in responsive
applications in java?
Selection of appropriate
garbage collector for the old generation space affects the length of the “Stop the World” event for a
major garbage collection.
3.3)
Permanent Generation or (Permgen) - (full garbage collection occurs in permanent generation in java).
·
Permanent generation
Space contains metadata required by JVM to describe the classes and methods
used in the application.
·
The permanent generation
is included in a full garbage collection in java.
·
The permanent generation
space is populated at runtime by JVM based on classes in use in the
application.
·
The permanent generation
space also contains Java SE library classes and methods in java.
·
JVM garbage collects
those classes when classes are no longer required and space may be needed for
other classes in java.
Configuring
VM (JVM)
PARAMETERS in JVM Heap memory
Total
Heap Size Configuration
Providing minimum heap size -Xms is minimum heap size
which is allocated at initialization of JVM.
Example1 Set the minimum heap size of JVM to
512 megabytes.
java
-Xms512m MyJavaProgram
Example2 Set the minimum heap size of JVM to 1
gigabyte.
java
-Xms1g MyJavaProgram
Providing maximum heap size -Xmx is the maximum heap
size that JVM can use.
Example1 Set the maximum heap size of JVM to
512 megabytes.
java
-Xmx512m MyJavaProgram
Example2 Set the maximum heap size of JVM to 1
gigabyte.
java
-Xmx1g MyJavaProgram
Young
Generation(VM PARAMETERS for Young Generation)
a)
Eden,
b)
S0 (Survivor space 0)
c)
S1 (Survivor space 1)
-Xmn : -Xmn sets the size
of young generation.
Example1 - java
-Xmn512m MyJavaProgram
Example2 - java
-Xmn1g MyJavaProgram
-XX:NewRatio :
NewRatio controls the size of young generation.
Example - -XX:NewRatio=3
means that the ratio between the young and old/tenured generation is 1:3.
In
other words, the combined size of the eden and survivor spaces will be one fourth of the total heap size.
-XX:NewSize -
NewSize is minimum size of young generation which is allocated at
initialization of JVM.
Note
: If you have specified -XX:NewRatio
than minimum size of the young generation is allocated automatically at
initialization of JVM.
-XX:MaxNewSize -
MaxNewSize is the maximum size of young generation that JVM can use.
-XX:SurvivorRatio : (for survivor space)
SurvivorRatio
can be used to tune the size of the survivor spaces, but this is often not as
important for performance.
Example:
-XX:SurvivorRatio=6 sets the ratio between
each survivor space and eden to be 1:6.
In
other words, each survivor space will be one
eighth of the young generation (not one seventh, because there are two
survivor spaces).
Old Generation (tenured) - (VM PARAMETERS
for Old Generation)
-XX:NewRatio
: NewRatio controls the size of young and old generation.
Example of
using -XX:NewRatio: -XX:NewRatio=3 means that the ratio between the young and
old/tenured generation is 1:3. In other words, the combined size of the eden
and survivor spaces will be one fourth of the total heap size.
Permanent Generation (VM PARAMETERS for
Permanent Generation)
-XX:PermSize:
It’s is initial value of Permanent Space which is allocated at startup of JVM.
Example1 :
Set initial value of Permanent Space as 512 megabytes to JVM
java
-XX:PermSize=512m MyJavaProgram
Example2 :
Set initial value of Permanent Space as 512 gigabyte to JVM
java
-XX:PermSize=1g MyJavaProgram
-XX:MaxPermSize:
It’s maximum value of Permanent Space that JVM can allot up to.
Example1 :
set maximum value of Permanent Space as 512 megabytes to JVM
java
-XX:MaxPermSize=512m MyJavaProgram
Example2 :
set maximum value of Permanent Space as 1 gigabyte to JVM
java
-XX:MaxPermSize=1g MyJavaProgram
Other
important VM (JVM) parameters for java heap in java
-XX:MinHeapFreeRatio and
-XX:MaxHeapFreeRatio
JVM
can grows or shrinks the heap to keep the proportion of free space to live
objects within a specific range.
-XX:+AggressiveHeap is
used for Garbage Collection Tuning setting.
This
VM option inspects the server resources and attempts to set various parameters
in optimal manner for long running and memory consuming applications. There
must be minimum of 256MB of physical memory on the servers before the
AggressiveHeap can be used.
-Xss - Use this VM option
to adjust the maximum thread stack size.
Also
you must know that -Xss option is same as -XX:ThreadStackSize.
Example1 :
set the default stack size of JVM to 512
megabytes.
java
-Xss512m MyJavaProgram
Example2 :
set the default stack size of JVM to 1
gigabyte.
java
-Xss1g MyJavaProgram
Different
types Garbage collectors
Serial collector / Serial GC (Garbage
collector) in java
·
Serial collector is also called Serial GC
(Garbage collector) or Serial Collector in java.
·
Serial GC (Garbage collector) is rarely used
in java.
·
Serial GC (Garbage collector) is designed for
the single threaded environments in java.
·
In Serial GC (Garbage collector) , both minor
and major garbage collections are done serially by one thread (using a single
virtual CPU) in java.
·
Serial GC (Garbage collector) uses a
mark-compact collection method. This method moves older memory to the beginning
of the heap so that new memory allocations are made into a single continuous
chunk of memory at the end of the heap. This compacting of memory makes it
faster to allocate new chunks of memory to the heap in java.
·
The serial garbage collector is the default
for client style machines in Java SE 5 and 6.
When to Use the Serial GC (garbage
Collector) in java?
·
The Serial GC is the garbage collector of
choice for most applications that do not have low pause time requirements and run
on client-style machines. It takes advantage of only a single virtual processor
for garbage collection work in java.
·
Serial GC (garbage collector) is also popular
in environments where a high number of JVMs are run on the same machine. In
such environments when a JVM does a garbage collection it is better to use only
one processor to minimize the interference on the remaining JVMs in java.
Vm (JVM) option for enabling serial GC
(garbage Collector) in java
-XX:+UseSerialGC
Example
of Passing Serial GC in Command Line for starting jar
java
-Xms256m -Xms512m -XX:+UseSerialGC -jar
d:\MyJar.jar
Throughput GC (Garbage collector) or
Parallel collector in java
·
Throughput collector is also called
o
Throughput GC (garbage collector)
o
ParallelGC (garbage collector)
o
Throughput collector
o
ParallelGC collector
·
Throughput garbage collector is the default
garbage collector for JVM in java.
·
Throughput garbage collector uses multiple
threads to execute a minor collection and so reduces the serial execution time
of the application in java.
·
The throughput garbage collector is similar
to the serial garbage collector but uses multiple threads to do the minor
collection in java.
·
This garbage collector uses a parallel
version of the young generation garbage collector in java.
·
The tenured generation collector is the same
as the serial garbage collector in java.
When
to Use the Throughput GC (Garbage collector) in java?
·
The Throughput garbage collector should be
used when application can afford low pauses in java.
·
And application is running on host with
multiple CPU’s (to derive advantage of using multiple threads for garbage
collection) in java.
Vm
(JVM) option for enabling throughput GC (Garbage collector) in java
-XX:+UseParallelGC
Example
of using throughput collector in Command Line for starting jar
java
-Xms256m -Xms512m -XX:+UseParallelGC -jar d:\MyJar.jar
With
this Vm (JVM) option you get a
·
Multi-threaded
young generation garbage collector in java,
·
single-threaded
old generation garbage collector in java and
·
single-threaded
compaction of old generation in java.
Vm
(JVM) option for enabling throughput collector with n number of threads in java
·
-XX:ParallelGCThreads=
Another
Vm (JVM) option for enabling throughput collector in java
·
-XX:+UseParallelOldGC
Goals for Throughput GC (Garbage
collector) in java
·
Maximum pause time goal (Highest priority)
·
Throughput goal
·
Minimum footprint goal (Lowest priority)
Controlling maximum pause time and throughput
Maximum
pause time goal (Highest priority)
·
Vm (JVM) option for maximum pause time in
java.
o
We gives hint to throughput collector that
pause time should be milliseconds or less.
o
-XX:MaxGCPauseMillis=
o
Example. We gives hint to throughput
collector that pause time should be 100 milliseconds or less. -XX:MaxGCPauseMillis=100
·
The throughput collector will adjust the Java
heap size and other garbage collection related parameters in an attempt to keep
garbage collection pauses shorter than milliseconds.
·
By default there is no maximum pause time
goal.
Throughput goal
·
Vm (JVM) option for throughput in java.
·
The throughput is measured time spent doing
garbage collection and time spent outside of garbage collection.
-XX:GCTimeRatio=
·
Example : XX:GCTimeRatio=19 sets a goal of 5%
of the total time for garbage collection. By default the goal for total time
for garbage collection is 1%.
·
The ratio of garbage collection time to
application time is 1 / (1 + )
Performance
of Throughput GC (garbage Collector) host with
different number of CPU’s in java
·
By default on a host with N CPUs, the
throughput collector uses N garbage collector threads in the minor collection.
Note : We can control number of garbage
collector threads with a command line option in java.
·
On a host with 1 CPU the throughput collector
will not perform as well as the serial collector because of the additional
overhead for the parallel execution (Example - Synchronization costs) in java.
·
On a host with 2 CPUs the throughput
collector generally performs as well as the serial garbage collector and a
reduction in the minor garbage collector pause times can be expected on hosts
with more than 2 CPUs in java.
Adjusting Generation Sizes in throughput
GC (Garbage collector)
·
By default a generation
o
grows in increments of 20% and
o
shrinks in increments of 5%.
·
Controlling growth of generation (in percent)
>
o
-XX:YoungGenerationSizeIncrement=
for the young generation and
o
-XX:TenuredGenerationSizeIncrement= for
the tenured generation.
·
shrink of generation (in percent) >
o
-XX:
AdaptiveSizeDecrementScaleFactor=
o
If the size of an increment for growing is
growthPercent, the size of the decrement for shrinking will be growthPercent/
shrinkPercent.
5.4) Concurrent Mark Sweep (CMS) Collector /
concurrent low pause collector in java
o concurrent low pause collector
o
concurrent low pause GC
(garbage collector)
o CMS GC (garbage Collector)
o
CMS Collector
o concurrent low pause collector
o
concurrent low pause GC
(garbage collector)
·
Concurrent Mark Sweep
(CMS) collector collects the old/tenured generation in java.
·
Concurrent Mark Sweep
(CMS) Collector minimize the pauses by doing most of the garbage
collection work concurrently with the application threads in java.
·
Concurrent Mark Sweep
(CMS) Collector action on live objects >
o
Concurrent Mark Sweep
(CMS) Collector does not copy or compact the live objects. A garbage
collection is done without moving the live objects. If fragmentation
becomes a problem, allocate a larger heap in java.
When to
Use the Concurrent Low Pause Collector in java
·
Concurrent Low Pause
Collector should be used if your applications that require low garbage
collection pause times in java.
·
Concurrent Low Pause
Collector should be used when your application can afford to share
processor resources with the garbage collector while the application is
running in java.
·
Concurrent Low Pause
Collector is beneficial to applications which have a relatively large set of
long-lived data (a large tenured generation) and run on machines with two
or more processors in java.
·
Examples
when to use
Concurrent Mark Sweep (CMS) collector / concurrent low pause collector
Example 1 - Desktop UI
application that respond to
events,
Example 2 - Web server
responding to a request
and
Example 3 - Database
responding to queries.
Vm (JVM) option for enabling Concurrent Mark
Sweep (CMS) Collector
·
-XX:+UseConcMarkSweepGC
Example of using Concurrent Mark Sweep (CMS) collector /
concurrent low pause collector
java -Xms256m -Xms512m
-XX:+UseConcMarkSweepGC -jar d:\MyJar.jar
Concurrent Mark Sweep (CMS) Collector /
concurrent low pause collector working in detail
As mentioned above
Concurrent Mark Sweep (CMS) collector collects the old/tenured generation
(i.e. performs Major garbage
collection process).
Major gc(garbage
collection) in Concurrent Mark
Sweep (CMS) Collector / concurrent low pause
·
For
each major collection the CMS collector will pause
all the application threads for a brief period at the beginning of
the collection and toward the middle of the collection.
·
The second pause tends
to be the longer than first pause and uses multiple threads to do the
collection work during that pause in java. The remainder of the collection
is done with a garbage collector thread that runs concurrently with the
application.
Minor gc (garbage
collection) in Concurrent Mark
Sweep (CMS) Collector / concurrent low pause collector
·
The minor collections is
done in a manner similar to the serial collector although multiple
threads are used to do the collection in java.
Heap Structure for CMS
garbage Collector
·
CMS garbage collectors
didies heap into three sections: young generation, old generation,
and permanent generation of a fixed memory size.
·
Young
Generation is further
divided into Eden, S0 (Survivor space 0) and S1
(Survivor space 1).
Detailed Steps in
GC (garbage collection) cycle in Concurrent Mark Sweep (CMS) Collector /
concurrent low pause garbage collector:
·
Young
Generation GC (garbage Collection)
o
Live
objects are copied from
the Eden space and survivor space to the other survivor space.
o Any older objects that have reached their
aging threshold are promoted to old generation.
·
After
Young generation GC (garbage Collection) in
java
o
After a young GC, the Eden
space and one of the survivor spaces is cleared. Eden, S0 and S1 are part
of Young Generation
o promoted objects (older objects that have
reached their aging threshold in young GC) are are available in old
generation.
- Old Generation
GC (garbage Collection) with CMS in java
- Initial mark phase - (First pause happens/ stop the
world event ) - mark live/reachable objects (Example - objects on
thread stack, static objects etc.) and elsewhere in the heap (Example
- the young generation).
- Concurrent marking phase
- (No pause phase ) - finds live objects while
the application continues to execute.
0.
Remark
- (Second pause
happens/ stop the world events) - It finds objects that were missed
during the concurrent marking phase due to the concurrent execution of the
application threads.
Old Generation GC (garbage Collection) -
Sweep phase (Concurrent Sweep phase) in java
1.
Sweep
phase - do the
concurrent sweep, memory is freed up.
·
Objects that were not
marked in the previous phase are deallocated in place.
·
There is no compaction
·
Unmarked
objects are equal to Dead Objects.
·
Old Generation GC
(garbage Collection) - After Sweeping
5.
Reset
phase - do the
concurrent reset.