Java Garbage Collection

2024-02-09

Index


Garbage Collection

Garbage collection in Java is a mechanism that automatically manages the memory used by a Java program (process to free up memory). Developers generally don’t need to explicitly free memory or manage the garbage collection process, as it is handled automatically by the JVM.

In this process memory is made available by deleting the objects which no longer can be referenced from stack or metaspace.

Metaspace - Used to store static objects references.

Simple Garbage Collection

System.gc()

Calling System.gc() in code to suggest the virtual machine to start the Garbage Collections but it may or may not start the garbage collection process immediately.

Changing size of heap

DescriptionVM CommandShortcutExample Usage
To set maximum heap size-XX:MaxHeapSize=n-Xmx-Xmx10m
To set initial heap size-XX:InitialHeapSize=n-Xms-Xms10m

Soft Leaks

When object remain referenced even when it no longer required is example of soft leak. This problem can easily happen in long running java application like servers.

Visualizing stack & heap memory using VisualVM application.

VisualVM is free application that can be downloaded from internet.

VisualVM

Heap Dump

  • Generating heap dump

We need to run our application by using below mentioned command. If application stopped working due to memory error it will generate heap dump at that place.

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=FILE_PATH

or it can be generate from VisualVM as well.

VisualVM

  • Viewing a heap dump

We can view heap dump by using tool MAT (Memory Analyzer tool) by eclipse.

MAT


Generational garbage collector

  • Introduction

If an object survives in a Garbage Collection process. Then it is most likely to live forever. To make this process efficient heap is organized in forms of generations.

Generational GC

Heap is divided into two general categories one is young generation and other is old generation. New objects are created inside young generation. The size of young generation is small as compare to old generation. Hence garbage collection in young space takes very less time as compare to the older generation. This process is also called minor garbage collection. Where as if old generation get filled and GC run over old generation objects. This takes few seconds time and also called major garbage collection.

  • Categories of Young Generation

Young generation types

Young generation is further divided into 2 Parts.

  • Eden
  • S0 (surviver 0)
  • S1 (surviver 1)

Eden is very small memory space where new objects get created and Garbage collection will run over it only. It will take very less time. The object that survived are then moved to S0. If they survived S0 as well then they are sent to S1. Objects are moved between these 3 partitions. If they survive 4-5 cycles then they are moved to old generation.

  • Viewing GC generations in VisualVM

Visual GC

We can view generation in VisualMV by enabling plugin Visual GC. We can monitor different generation as shown.



More posts like this

Java Garbage Collector Tuning and Selection

2024-02-10 | #Garbage Collection #Java #JVM tunning #Performance & optimization

Index Monitoring Garbage collections Turning off automated heap allocation sizing Tuning garbage collection Old and Young allocation Survivor space allocation Generation needed to become old Selecting a garbage collector Types G1 garbage collector Tuning the G1 garbage collector String de-duplication Misc Viewing running java processes and PID Viewing enabled flag on a process Monitoring Garbage collections We can pass -verbose:gc VM argument to monitor the garbage collection.

Continue reading 


Hello World

2023-05-23 | #AWS #Java #Leetcode #Node.js

Hello World, I started this blog to share my daily learning progress. I love to solve daily Leetcode challenges. I use Java, Nodejs and AWS for backend development. You can find more about me in About section.

Continue reading 