DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • All You Need To Know About Garbage Collection in Java
  • JVM Memory Architecture and GC Algorithm Basics
  • Java Memory Management
  • Optimizing Java Applications for Arm64 in the Cloud

Trending

  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • Your AI Agent Tests Are Passing, But Your Agent Is Still Broken
  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  1. DZone
  2. Coding
  3. Languages
  4. Garbage Collection in Java (JVM)

Garbage Collection in Java (JVM)

Garbage Collection in Java automatically allocates and deallocates memory, so that developers don’t need to write an explicit program to do memory management.

By 
Arjun P Jathindramohan user avatar
Arjun P Jathindramohan
·
Jan. 22, 21 · Analysis
Likes (6)
Comment
Save
Tweet
Share
8.1K Views

Join the DZone community and get the full member experience.

Join For Free

Garbage Collection Process in Java

Garbage Collection in Java automatically allocates and deallocates memory, so that developers don’t need to write an explicit program to do memory management, which is one of the main advantages of Java programming.

Whenever a Java program runs on the JVM, the objects are created on the heap and are a portion of memory that is dedicated to the program. Eventually, some objects will no longer be needed. The garbage collector finds these unused objects and deletes them to free up memory.

The garbage collector will look at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. An in-use object, or a referenced object, means that some part of your program still maintains a pointer to that object.
An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory which is used by an unreferenced object can be reclaimed by performing a Garbage Collection.


generation diagram

Deallocation of memory can be described in 3 basic processes:

  1. Marking
  2. Normal Deletion
  3. Deletion with Compacting

Marking — Process of identifying the pieces of memory which are in use and are not by Garbage collector and is the first step

Normal Deletion — Process of removing unreferenced objects leaving referenced objects and pointers to free space.

Deletion with Compacting — In addition to deleting unreferenced objects, it will compact the remaining the referenced objects by moving the objects together to make the
new memory allocation much easier and faster.

JVM Heap Memory

JVM Heap Memory diagram

Young Generation

Newly created objects start in the Young Generation. The young generation is also called nursery as the new object start living here. The Young Generation is further subdivided into an Eden space, where all new objects start, and two Survivor spaces, where objects are moved from Eden after surviving one garbage collection cycle. These are call Minor garbage collection event when objects are garbage collected from the Young Generation,

  1. Eden Space
    All new objects are first created in the Eden Space. A minor GC collection will kicks in when it reaches a threshold that JVM decides. Referenced objects are moved from Eden space to the first survivor space(‘Eden’ and ‘from’ —> ‘to’).  Unreferenced objects are deleted when the Eden space is cleared.
  2. Survivor 0 (S0) and Survivor 1 (S1)
    Both survivor spaces(From and to) start out empty. When a minor GC collection happens all the referenced objects are moved to survivor space. Once the GC is over, the Survivor spaces ‘from’ and ‘to’ roles (names) are swapped. S1 was the ‘to’ role during the previous garbage collection (GC). Now S1 is filled and takes the ‘from’ role and S0 is empty and will take the ‘to’ role.

Old Generation

After a minor GC, when aged objects reach a certain age threshold(By default, modern JVMs threshold is set to 15 GC cycles) they are promoted from the young generation to the old generation. Objects that are long-lived are eventually moved from the Young Generation to the Old Generation. As minor GCs continue to occur, objects will continue to be promoted to the old generation space and it will start getting filled and a Major GC will occur. Major garbage collection will happen when the objects are garbage collected from the Old Generation.

Garbage collection diagram


Permanent Generation

Metadata such as classes and methods are stored in the Permanent Generation. Classes that are no longer in use may be garbage collected from the Permanent Generation. During a full garbage collection event, unused objects in all generations are garbage collected.
GC Process Summary

Types of Garbage Collection (GC)

The Garbage Collection events cleaning out different parts inside heap memory are often called Minor, Major, and Full GC events. But, as the terms Minor, Major, and Full GC are widely used and without a proper definition, we will have a look at the explanation of all these GC.

Minor GC

Collecting garbage from the Young Generation space is called Minor GC. Minor GC cleans the Young Generation. Minor GC is always triggered when the JVM is unable to allocate space for a new object, i.e., when the Eden space is getting full.  So the higher the allocation rate, the more frequently Minor GC occurs.

Major GC

Major GC is cleaning the Tenured (Old space). As OLD Gen is bigger in size, the GC occurs less frequently than in the young generation. When objects disappear from the old generation, we say a 'major GC' has occurred. The old generation collector will try to predict when it needs to collect to avoid a promotion failure from the young generation.
The collectors track a fill threshold for the old generation and begin collection when this threshold is passed.  If this threshold is not sufficient to meet promotion requirements then a 'FullGC' is triggered.

Full GC

Full GC is cleaning the entire Heap — both Young and Old spaces. Many people get confused with Major (Only OLD generation) and Full GC (Young + OLD(Heap)). A FullGC involves promoting all live objects from the young generation to the OLD generation after collection and compaction of the old generation.  Full GC will be a Stop-the-World pause. Stop-the-World is making sure that new objects are not allocated and objects do not suddenly become unreachable while the collector is running.

Summary

To identify the Heap Memory usage using Jmap and Jstat: Please refer to the below article. 

https://javaperformance.co.in/2017/02/05/java-heap-memory-usage-using-jmap-and-jstat-command/

Please refer to below for Garbage collection:

https://javaperformance.co.in/2017/07/05/garbage-collection-in-java/

garbage collection Java (programming language) Garbage (computer science) Java virtual machine Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • All You Need To Know About Garbage Collection in Java
  • JVM Memory Architecture and GC Algorithm Basics
  • Java Memory Management
  • Optimizing Java Applications for Arm64 in the Cloud

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook