Understand Garbage Collection in java
arpitmandliya
Posted on July 16, 2019
In this post, we will see about Garbage Collection in java.
I will try to explain with the help of diagrams and examples rather than theory.
JVM Memory is divided into three parts
- Young generation
- Old generation
- Metaspace (Perm Gen)
Young Generation
As the name suggests, young generation is the area where newly created objects are allocated.
- When young generation fills up, it cause minor garbage collection aka Minor GC.
- When minor Gcs occurs, dead objects will be removed from young generation.
- If you have lot of dead objects in young generation, Minor GC will be perfomed faster.
- All minor GCs are "stop the world" events, so when minor GCs occurs, application threads will also stop.
Let's understand more about how objects are allocated in Young generation.
Young generation is divided into 3 parts.
- Eden space
- Survivor space S0
- Survivor space S1
Here is illustration of the young and old generation.
-
- All newly created objects are allocated in eden space.
- When Eden space is completely filled with objects then minor GC will occur. All the objects which are not dead or unreferenced will be moved to one of the survivors spaces. In our case, let's say all the objects are moved to S0.
-
- When Eden space is filled again, then all the live objects in Eden space andSurvivor space S0 will be moved to Survivor space S1.
- Once objects are survived multiple cycles of minor GC, they will be moved to old generation. You can control this threshold by MaxTenuringThreshold. The actual tenuring threshold is dynamically adjusted by JVM.
Let's try to demonstrate this behavior with the help of example:
You have below class in which I am creating short-lived objects for demonstration.
package org.arpit.java2blog; import java.math.BigDecimal; public class GCCollectorExample { public static void main(String args[]) { createObjects(); } private static void createObjects() { long count = 0; while(true) { // Creating short-lived objects just for illustration BigDecimal shortLivedBigDecimal1 = new BigDecimal(count++); } } }
When you run the program. Let's check memory allocation in Visual GC (A visualVM plugin)
As you can see, we have few objects in S0 and once eden space is completely filled, all referenced objects will be moved to S1.
Old generation
- It is used to hold old long surviving objects
- It is generally larger than the young generation.
- When tenured space is completely filled(or predefined threshold) with objects then Major GC will occur. It will reclaim the memory and free up space.
- Often, Major GCs are slower and less frequent than minor GC.
How can you use this information to optimize memory?
It depends on nature of application.
If you have lots of temporary objects then there will be lot of minor gc. You can provide arguments XX:NewRatio=1 to distribute 50% to young generation and 50% to old.
By default, NewRatio=2 hence young Generation is 1/3 of total heap.
Similarly, If you have too many long-lived objects, then you might need to increase size of tenure space by putting high value of NewRatio.
Why two survivor spaces?
You must be wondering why do we have 2 survivor space. We have 2 survivor spaces to avoid memory fragmentation. Each time you copy objects from eden to survivor and you get empty eden space and 1 empty survivor space.
Garbage Collection Algorithms
JVM comes with several algorithms for young and old generation. There are 3 types of algorithms
serial collector
It uses single thread to perform all the garbage collection and is suitable for basic application with single processor machines.
Parallel collector
It uses multiple CPUs to perform garbage collector. While serial collector uses 1 thread to perform GC, parallel GC uses several threads to perform GC and it is useful when there is enough memory and good number of cores.
Concurrent collector
Concurrent collector performs garbage collection with application threads. It is useful for applications which have medium to large datasets and require quick response time.
You can use different GC algorithms for young and old generations but you can pair only compatible algorithms.
For example: You can not pair Parallel Scavenge for young generation to Concurrent mark sweep for old as parallel scavenge does not provide synchronization which is required in CMS.
Here are garbage collectors which you can use for young and old generation.
[table id=17 /]
There are a lot of changes in garbage collectors after Java 8, I will try to cover them in my next articles.
If you have any suggestions on this article, please comment.
Original post:
Garbage Collection in java
You may also like:
Posted on July 16, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 7, 2024