In the JVM world if you talked about Java Memory Management then first you want to understand the working of Java Garbage Collection. While working on JVM based application it becomes super easy when JVM itself manages your GC for you but the problem starts when you start facing performance degradation and that too because of in-efficient GC.
So let’s start with understanding what is JVM GC model and then we can see how to control it and analyse the GC logs to find any discrepancies occurring in your application.
“”” What is Automatic Garbage Collection? “””
“Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects.”
Automatic GC Steps
– The first step in the process is called marking. This is where the garbage collector identifies which pieces of memory are in use and which are not.
– This can be a very time-consuming process if all objects in a system must be scanned.
2. Normal Deletion
Normal deletion removes unreferenced objects leaving referenced objects and pointers to free space.
3. With Compaction
To further improve performance, in addition to deleting unreferenced objects, you can also compact the remaining referenced objects.
By moving the referenced object together, this makes new memory allocation much easier and faster.
All Automatic – Why It’s a Problem then
It stops the world event, Stops the world event means, Nothing happen in the application when Garbage Collection is happening and the app is unresponsive for that period. So GC should take minimum time, if it is taking more time, then there is a problem with your app GC settings or App.
Below is the JVM memory Model and it is divided into separate parts. At the basic level, JVM Heap memory is physically divided into two parts – the Young Generation and Old Generation.
1. First, any new objects are allocated to the Eden space. Both survivor spaces start out empty.
2. When the Eden space fills up, a minor garbage collection is triggered.
3. Referenced objects are moved to the first survivor space. Unreferenced objects are deleted when the Eden space is cleared.
4. At the next minor GC, the same thing happens for the Eden space. Unreferenced objects are deleted and referenced objects are moved to a survivor space. However, in this case, they are moved to the second survivor space (S1).
5. This slide demonstrates promotion. After a minor GC, when aged objects reach a certain age threshold (8 in this example) they are promoted from the young generation to the old generation.
6. So that pretty much covers the entire process with the young generation. Eventually, a major GC will be performed on the old generation which cleans up and compacts that space.
Ways to Analyse the GC Logs –
2) GC Easy – https://gceasy.io/
3) GC Log Viewer – https://gclogviewer.javaperformancetuning.co.za/#
4) GC Plot Tool for Linux – https://gcplot.com/
That all for now for the GC basics, In some other blog we will be focusing more on different GC strategies and what all setting one should use and how.