Garbage Collection in java

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.

YoungOldGeneration

    • 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.


S0AllocatedObjects

    • When Eden space is filled again, then all the live objects in Eden space andSurvivor space S0 will be moved to Survivor space S1.

S0T0S1AlloactedObjects
S1AllocatedObjects

  • 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.

When you run the program. Let’s check memory allocation in Visual GC (A visualVM plugin)

S0AndEdenDemonstation-1024x422

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.

S1FilledAndEdenDemonstration


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.

SerialGC

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.

ParallelGC

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.

concurrentGC

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.

Young CollectorOld CollectorJVM options
Serial (DefNew)Serial Mark-Sweep-Compact-XX:+UseSerialGC
Parallel scavenge (PSYoungGen)Serial Mark-Sweep-Compact (PSOldGen)-XX:+UseParallelGC
Parallel scavenge (PSYoungGen)Parallel Mark-Sweep-Compact (ParOldGen)-XX:+UseParallelOldGC
Serial (DefNew)Concurrent Mark Sweep-XX:+UseConcMarkSweepGC 
-XX:-UseParNewGC
Parallel (ParNew)Concurrent Mark Sweep-XX:+UseConcMarkSweepGC 
-XX:+UseParNewGC
G1-XX:+UseG1GC

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.

Was this post helpful?

Comments

  1. nice article. keep going buddy..

    please share articles on system designing like how anaysis and design is important and how to build it from scratch using java tech stack

Leave a Reply

Your email address will not be published. Required fields are marked *