Understanding Garbage Collection in java — Java2Blog

Arpit Mandliya
Sep 17, 2019 · 4 min read

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

Image for post
  • 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.
Image for post
Image for post
Image for post
Image for post

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)

Image for post

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.

Image for post

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

serial collector

It uses a single thread to perform all the garbage collection and is suitable for basic application with single processor machines.

Image for post

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 are enough memory and a good number of cores.

Image for post

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.

Image for post

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.

You may also like:

Javarevisited

Medium’s largest Java publication, followed by 9300+ programmers. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store