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)
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 performed 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.
Here is an 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.
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.
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.
- 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 the nature of the 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
It uses a single thread to perform all the garbage collection and is suitable for basic application with single processor machines.
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 are enough memory and a good number of cores.
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 the young generation to Concurrent mark sweep for old as parallel scavenge does not provide synchronization which is required in CMS.
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.