Garbage collection is no doubt an automatic feature provided by JVM but still for making your application performant one need to dig into it.
In this knolx, we will be talking about how actually GC is performed and what are its various GC Generations. Then we will look into different GC strategies evolved in time and at the end will look into the demo of how to visualize the GC logs and tune up the GC strategies in the sample java app.
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
Are You Managing Your Garbage Correctly?
1. Presented By: Piyush Rana (Lead Consultant @ Knoldus)
Are you managing your
Garbage correctly?
2. Agenda
01 What is Automatic Garbage
Collection?
02 JVM Generations
03 GC Strategies
04 Demo
Question And Answers??
60
6
0404
05
3. What is Automatic Garbage Collection?
“Automatic garbage collection is the
process of looking at heap memory,
identifying which objects are in use
and which are not, and deleting the
unused objects.”
4. Automatic GC Steps
1. Marking
- The first step in the process is called
marking. This is where the garbage
collector identifies which pieces of
memory are in use and which are not.
- This can be a very time consuming
process if all objects in a system must be
scanned.
5. Automatic GC Steps –
2. Normal Deletion
Normal deletion removes unreferenced objects leaving referenced objects and
pointers to free space.
6. Automatic GC Steps
3. With Compaction
To further improve performance, in addition to
deleting unreferenced objects, you can also
compact the remaining referenced objects.
By moving referenced object together, this
makes new memory allocation much easier
and faster.
7. Why Generational Garbage Collection?
The Y axis shows the number of bytes
allocated and the X access shows the
number of bytes allocated over time.
As you can see, fewer and fewer
objects remain allocated over time. In
fact most objects have a very short life
as shown by the higher values on the
left side of the graph.
8. JVM Generations
The Young Generation is where all new objects are allocated and aged. When the young
generation fills up, this causes a minor garbage collection.
The Old Generation is used to store long surviving objects. Typically, a threshold is set for
young generation object and when that age is met, the object gets moved to the old
generation. This event is called a major garbage collection.
The Permanent generation contains metadata required by the JVM to describe the classes
and methods used in the application.
9. All Automatic - Why It’s a Problem then
It is stop the world event , Stop the world event means ,Nothing happen in
application when Garbage Collection is happening and app is
unresponsive for that period. So GC should take minimum time, if it is
taking more time, then there is problem with your app GC settings or App.
10. How the Different Generations Interact -
1. First, any new objects are allocated to the eden space. Both survivor
spaces start out empty.
2. When the eden space fills up, a minor garbage collection is triggered.
11. How the Different Generations Interact -
3. Referenced objects are moved to the first survivor space. Unreferenced objects
are deleted when the eden space is cleared.
4. At the next minor GC, the same thing happens for the eden space.
Unreferenced objects are deleted and referenced objects are moved to a survivor
space. However, in this case, they are moved to the second survivor space (S1).
12. How the Different Generations Interact -
5. This slide demonstrates promotion. After a minor GC, when aged objects reach
a certain age threshold (8 in this example) they are promoted from young
generation to old generation.
6. So that pretty much covers the entire process with the young generation.
Eventually, a major GC will be performed on the old generation which cleans up
and compacts that space.
13. GC STRATEGIES
1. Serial GC (-XX:+UseSerialGC)
2. Parallel GC (-XX:+UseParallelGC)
3. Parallel Old GC (-XX:+UseParallelOldGC)
4. Concurrent Mark Sweep (CMS) Collector (-
XX:+UseConcMarkSweepGC)
5. G1 Garbage Collector (-XX:+UseG1GC)
14. 1. Serial GC (-XX:+UseSerialGC)
- Serial GC uses the simple mark-sweep-compact approach for young
and old generations garbage collection i.e Minor and Major GC.
- It is good for small applications with low memory footprint.
- The serial collector is the default for client style machines in Java SE 5
and 6. With the serial collector, both minor and major garbage collections
are done serially (using a single virtual CPU).
Lets test - java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -
XX:MaxPermSize=20m -XX:+UseSerialGC -XX:+PrintGCTimeStamps -
Xloggc:gc-serial.log -jar Java2Demo.jar
GC STRATEGIES
15. 2. Parallel GC (-XX:+UseParallelGC)
- The parallel garbage collector uses multiple threads to perform the
young genertion garbage collection.
- By default on a host with N CPUs, the parallel garbage collector uses N
garbage collector threads in the collection. The number of garbage
collector threads can be controlled with command-line options:
-XX:ParallelGCThreads=<desired number>
3. Parallel Old GC (-XX:+UseParallelOldGC)
With the -XX:+UseParallelOldGC option, the GC is both a multithreaded young generation
collector and multithreaded old generation collector. It is also a multithreaded compacting
collector
GC STRATEGIES
16. 4. Concurrent Mark Sweep (CMS) Collector (-
XX:+UseConcMarkSweepGC)
- It collects the tenured generation. It attempts to minimize the pauses
due to garbage collection by doing most of the garbage collection work
concurrently with the application threads.
- The CMS collector should be used for applications that require low
pause times and can share resources with the garbage collector.
Examples include desktop UI application that respond to events, a
webserver responding to a request or a database responding to queries.
GC STRATEGIES
17. 5. G1 Garbage Collector (-XX:+UseG1GC)
- The Garbage First or G1 garbage collector is available in Java 7
and is designed to be the long term replacement for the CMS
collector.
- The G1 collector is a parallel, concurrent, and incrementally
compacting low-pause garbage collector that has quite a different
layout from the other garbage collectors described previously.
- Lets Test java -Xmx24m -XX:+UseG1GC -Xloggc:gc-g1gc.log -jar
Java2Demo.jar
GC STRATEGIES
18. Some Other Setting which one needs to look for
application tunning -
-XX:MaxGCPauseMillis: Sets a target for the maximum GC pause time. This is a soft goal, and the JVM will make
its best effort to achieve it.
-XX:ParallelGCThreads: Sets the number of threads used during parallel phases of the garbage collectors. The
default value varies with the platform on which the JVM is running.
-XX:ConcGCThreads: Number of threads concurrent garbage collectors will use. The default value varies with the
platform on which the JVM is running.
-XX:InitiatingHeapOccupancyPercent: Percentage of the (entire) heap occupancy to start a concurrent GC cycle.
A value of 0 denotes 'do constant GC cycles'. The default value is 45.
And other like -XX:+UnlockExperimentalVMOptions -XX:+ParallelRefProcEnabled -XX:+DisableExplicitGC
GC PARAMETERS
19. Ways to Analyse the GC Logs
1) VisualVm
2) GC Easy - https://gceasy.io/
3) GC Log Viewer - https://gclogviewer.javaperformancetuning.co.za/#
4) GC Plot Tool for Linux - https://gcplot.com/
GC LOGS VISUALIZATION TOOLS -