Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Did You Manage Your Garbage Efficiently?

DZone 's Guide to

Did You Manage Your Garbage Efficiently?

It's time to take the trash out.

· Java Zone ·
Free Resource

In the JVM world, if you talked about Java memory management, then first, you want to understand the workings of Java garbage collection. While working on a JVM-based application, it becomes super easy when the JVM, itself, manages your GC for you, but the problem starts when you start facing performance degradation and that, too, is because of in-efficient GC.

So, let’s start by understanding what is the JVM GC model, and then, we can see how to control it and analyze 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

1. Marking

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.

marking.png

2. Normal Deletion

deletion.png

Normal deletion removes unreferenced objects leaving referenced objects and pointers to free space.

3. With Compaction

compac

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

problem.png

It stops the world event, stops the world event means that nothing happens 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.

Java-Memory-Model

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 Analyze the GC Logs

1) VisualVm

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's all for now for the GC basics. In some other blogs, we will be focusing more on different GC strategies and what all setting one should use and how. Stay tuned!

This article was first published on the Knoldus blog.

Topics:
java ,garbage ,garbage collection ,gc ,garbage management ,memory management ,jvm ,memory

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}