You are on page 1of 26

Garbage Collector (GC)

Coding & Fun : By Ankit Gupta


Table of contents:
 What is Garbage Collector (GC) ?
 How does Garbage Collector work inside JVM ?
 What is Mark and Sweep Algorithm ?
 What is Mark-Sweep-Compact Algorithm ?
 What are the differences between Minor, Major and Full GC ?
 What are different types of Garbage Collector ?
1. What is Serial GC ?
2. What is Parallel GC ?
3. What is Parallel Old GC ?
4. What is Concurrent Mark Sweep Collector ?
5. What is G1 GC ?
 Why Garbage Collector plays very important role inside JVM ?
 What is compaction in GC ?
What is Mark and Sweep Algorithm
The two phases of Java Garbage Collection Algorithm are:
• Mark Phase
• Sweep Phase
What is Mark Phase ?
 Every modern GC algorithm used in JVM starts its job with finding out all objects that are still alive. This
concept is best explained using the following picture representing your JVM’s memory layout:

 GC defines some specific objects as Garbage Collection Roots


 GC traverses the whole object graph in your memory
 Every object the GC visits is marked as alive or set flag true, represented as blue(live Objects).
 Such a situation when the application threads are temporarily stopped so that the JVM can indulge in
housekeeping activities is called a safe point resulting in a Stop The World pause.
What is Sweep Phase ?
 As the name suggests it “sweeps” the unreachable objects i.e. it clears the heap memory
for all the unreachable objects.
 after the marking phase has completed all space occupied by unvisited objects is
considered free and can thus be reused to allocate new objects.
 Below diagram shows occupied (Blue) and free space ( Grey).
 After sweep phase you can see free space in between allocated space.
 The approach requires using the so called free-list recording of every free region and its
size.
 The management of the free-lists adds overhead to object allocation. Built into this
approach is another weakness – there may exist plenty of free regions but if no single
region is large enough to accommodate the allocation, the allocation is still going to fail
(with an OutOfMemoryError in Java).
Table of contents:
 What is Garbage Collector (GC) ?
 How does Garbage Collector work inside JVM ?
 What is Mark and Sweep Algorithm ?
 What is Mark-Sweep-Compact Algorithm ?
 What are the differences between Minor and Major GC ?
 What are different types of Garbage Collector ?
1. What is Serial GC ?
2. What is Parallel GC ?
3. What is Parallel Old GC ?
4. What is Concurrent Mark Sweep Collector ?
5. What is G1 GC ?
 Why Garbage Collector plays very important role inside JVM ?
 What is compaction in GC ?
What is Mark-Sweep-Compact Algorithm
 Mark-Sweep-Compact algorithms solve the shortcomings of Mark and Sweep by moving
all marked – and thus alive – objects to the beginning of the memory region.
 The downside of this approach is an increased GC pause duration as we need to copy all
objects to a new place and to update all references to such objects.
 The benefits to Mark and Sweep are also visible – after such a compacting operation new
object allocation is again extremely cheap via pointer bumping. Using such approach the
location of the free space is always known and no fragmentation issues are triggered
either.
Table of contents:
 What is Garbage Collector (GC) ?
 How does Garbage Collector work inside JVM ?
 What is Mark and Sweep Algorithm ?
 What is Mark-Sweep-Compact Algorithm ?
 What are the differences between Minor, Major and Full GC ?
 What are different types of Garbage Collector ?
1. What is Serial GC ?
2. What is Parallel GC ?
3. What is Parallel Old GC ?
4. What is Concurrent Mark Sweep Collector ?
5. What is G1 GC ?
 Why Garbage Collector plays very important role inside JVM ?
 What is compaction in GC ?
Minor Vs Major Vs Full Garbage Collection (GC)
 Before learning about Minor, Major and Full garbage collection in java we must know about
JVM Heap memory

 Heap Memory is divided in to below categories:


1. Young Generation
1a) Eden,
1b) S0 (Survivor space 0)
1c) S1 (Survivor space 1)
2. Old Generation (Tenured)
3. Permanent Generation.
What is Minor GC ?
 Minor garbage collection occurs in Young Generation.
 Minor GC is always triggered when the JVM is unable to allocate space for a new object,
e.g. Eden is getting full.

 The JVM copies the reachable objects to “To Space”, S0, and increments the reachable
object's age by 1.
Minor GC
No Space
For me
E
D
Max Tenuring E
Threshold : 15 N
to specify the
object age 1 1 1 1 1 1 1 1
threshold to
promote the
S S
object to
0 1 1 1 2 2 2
tenured space

TENURED SPACE
What is Major GC ?
If minor GC triggers several times, eventually the “Tenured Space” will be filled up and will
require more garbage collection. During this time the JVM triggers a “major GC” event. Some
times we call this full GC. But, as part of full GC, the JVM reclaims the memory from “Meta
Space”. If there are no objects in the heap, then the loaded classes will be removed from the
meta space.
Now let us see what possibilities make the JVM trigger major GC.
 If the developer calls System.gc(), or Runtime.getRunTime().gc() suggests the JVM to
initiate GC.
 If the JVM decides there is not enough tenured space.
 During minor GC, if the JVM is not able to reclaim enough memory from the eden or
survivor spaces, then a major GC may be triggered.
 If we set a “MaxMetaspaceSize” option for the JVM and there is not enough space to load
new classes, then the JVM triggers a major GC.
I am a
Premature Promotion bigger size
object
E
D
E
Promoted N
to
Tenured Space
2 2 4 2 No space
S S for me in
0 1 young
generation
3 3 2 3 space
I need
space in
tenured
generation

TENURED SPACE
Major GC vs Full GC

 One should notice that there is no formal definitions present for those terms. Neither in
JVM specification nor in the Garbage Collection research papers.
 Major GC is cleaning the Tenured space.
 Full GC is cleaning the entire Heap – both Young and Tenured spaces.
 Instead of worrying whether the GC is called Major or Full GC, you should focus to
finding out whether the GC at hand stopped all the application threads or was it able to
progress concurrently with the application threads.
Table of contents:
 What is Garbage Collector (GC) ?
 How does Garbage Collector work inside JVM ?
 What is Mark and Sweep Algorithm ?
 What is Mark-Sweep-Compact Algorithm ?
 What are the differences between Minor, Major and Full GC ?
 What are different types of Garbage Collector ?
1. What is Serial GC ?
2. What is Parallel GC ?
3. What is Parallel Old GC ?
4. What is Concurrent Mark Sweep Collector ?
5. What is G1 GC ?
 Why Garbage Collector plays very important role inside JVM ?
 What is compaction in GC ?
What are different types of Garbage Collector ?
1. Serial Garbage Collector
2. Parallel Garbage Collector
3. Parallel Old Garbage Collector
4. Concurrent Mark Sweep Garbage Collector
5. G1 Garbage Collector
What is Serial Garbage Collector ?
 The serial collector is the simplest one, and the one you probably won’t be using, as it’s
mainly designed for single-threaded environments (e.g. 32 bit or Windows) and for small
heaps.
 This is the simplest GC implementation, as it basically works with a single thread. As a
result, this GC implementation freezes all application threads when it runs. Hence, it is
not a good idea to use it in multi-threaded applications like server environments.
 To enable Serial Garbage Collector, we can use the following argument:

java -XX:+UseSerialGC -jar Application.java


What is Parallel Garbage Collector ?
 Parallel garbage collector is also called as throughput collector.
 This is the JVM’s default collector. Much like its name, its biggest advantage is that is uses
multiple threads to scan through and compact the heap.
 The downside to the parallel collector is that it will stop application threads when
performing either a minor or full GC collection. The parallel collector is best suited for
apps that can tolerate application pauses and are trying to optimize for lower CPU
overhead caused by the collector.
 To enable Parallel Garbage Collector, we can use the following argument:

java -XX:+UseParallelGC -jar Application.java


What is Concurrent Mark Sweep Collector ?
 Concurrent Mark Sweep (CMS) garbage collector uses multiple threads to scan the heap
memory to mark instances for eviction and then sweep the marked instances. CMS
garbage collector holds all the application threads in the following two scenarios only,
1. while marking the referenced objects in the tenured generation space.
2. if there is a change in heap memory in parallel while doing the garbage collection.
 In comparison with parallel garbage collector, CMS collector uses more CPU to ensure
better application throughput. If we can allocate more CPU for better performance then
CMS garbage collector is the preferred choice over the parallel collector.
 To enable the CMS Garbage Collector, we can use the following flag:

java -XX:+UseParNewGC -jar Application.java


What is G1 Garbage Collector ?
 The older garbage collectors (serial, parallel, CMS) all structure the heap into three sections: young
generation, old generation, and permanent generation of a fixed memory size.
 All memory objects end up in one of these three sections.

 G1 performs a concurrent global marking phase to determine the liveness of objects throughout the
heap.

 After the marking phase completes, G1 knows which regions are mostly empty. It collects these regions
first, which often yields a large amount of free space.
 G1 uses a pause prediction model to meet a user-defined pause time target and selects the number of
regions to collect based on the specified pause time target.
 G1 copies objects from one or more regions of the heap to a single region on the heap, and in the process
both compacts and frees up memory.
 This evacuation is performed in parallel on multiprocessors to decrease pause times and increase
throughput.
 G1 continuously works to reduce fragmentation. This is beyond the capability of both of the previous
methods. CMS (Concurrent Mark Sweep) garbage collection does not do compaction. Parallel compaction
performs only whole-heap compaction, which results in considerable pause times.

 The first focus of G1 is to provide a solution for users running applications that require large heaps with
limited GC latency. This means heap sizes of around 6 GB or larger, and a stable and predictable pause time
below 0.5 seconds
 G1 is planned as the long-term replacement for the Concurrent Mark-Sweep Collector (CMS).
 One difference is that G1 is a compacting collector. Also, G1 offers more predictable garbage collection
pauses than the CMS collector, and allows users to specify desired pause targets.
Which Application needs G1 Garbage Collector ?
 The G1 collector is designed for applications that:
 Can operate concurrently with applications threads like the CMS collector.
 Compact free space without lengthy GC induced pause times.
 Need more predictable GC pause durations.
 Do not want to sacrifice a lot of throughput performance.
 Do not require a much larger Java heap.
 The Garbage-First (G1) collector is a server-style garbage collector, targeted for multi-
processor machines with large memories
Table of contents:
 What is Garbage Collector (GC) ?
 How does Garbage Collector work inside JVM ?
 What is Mark and Sweep Algorithm ?
 What is Mark-Sweep-Compact Algorithm ?
 What are the differences between Minor, Major and Full GC ?
 What are different types of Garbage Collector ?
1. What is Serial GC ?
2. What is Parallel GC ?
3. What is Parallel Old GC ?
4. What is Concurrent Mark Sweep Collector ?
5. What is G1 GC ?
 Why Garbage Collector plays very important role inside JVM ?
 What is compaction in GC ?
Why Garbage Collector plays very important role inside JVM ?
What is compaction in GC ?
https://dzone.com/articles/understanding-
the-java-memory-model-and-the-garbag

 The objects which are not fit into the survivor space will be moved to the tenured
space. This process is called “premature promotion”

You might also like