Assignments/Article By Silvia Scano

Garbage Collection in Java

Student Assignment

Flowchart

Published on 22, 2022

This article explain the Garbage Collection in Java, and it’s benefit .and to some extent illustrates it’s work mechanism.

What is Garbage collection:

Garbage collection in Java is the process of automatically freeing heap memory by deleting unused objects that are no longer accessible in the program. It also frees up developers from having to manually manage a program's memory, which, in turn, reduces the potential for memory-related bugs.

Conditions for a garbage collection

Garbage collection occurs when one of the following conditions is true:
  • The system has low physical memory. The memory size is detected by either the low memory notification from the operating system or low memory as indicated by the host.
  • The memory that's used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.
  • The GC.Collect method is called. In almost all cases, you don't have to call this method because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

How Does Garbage Collection in Java works?

Java garbage collection is an automatic process. 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.

An in-use object, or a referenced object, means that some part of your program still maintains a pointer to that object.

An unused or unreferenced object is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed. Such as :
  • When we explicitly assign null to the reference variable
  • When we assign one reference variable to another reference variable
  • When the reference variable is out of scope then the object referred by that reference variable is unused
Object type Assignment

Ways for Invoking Garbage Collector (GC)

When the unused object becomes eligible for garbage collection, garbage collector does not destroy them immediately. JVM runs garbage collector whenever it runs low in memory.

There are two methods for requesting JVM to run garbage collector. They are as follows:

Using Runtime.getRuntime().gc() method

Using System.gc() method

Student Assignment

The views expressed in this document are those of the author and do not necessarily reflect the position of the London School of Emerging Technology. View the detailed policy Disclaimer for Student and Personal Websites

Our Latest Blog

Beyond the Buzz: How Prompt Engineering is Quietly Reshaping the Modern Workplace

Beyond the Buzz: How Prompt Engineering is Quietly Reshaping the Modern Workplace

The future of work isn’t about being replaced by AI, it’s about learning how to...
Read More
What is GPT Understanding Generative Pre-trained Transformers Mayur Ramgir

What is GPT? Understanding Generative Pre-trained Transformers

In the world of artificial intelligence (AI), a family of models known as GPT has...
Read More
How to Become an AI-Powered DevOps or Cloud Engineer in 2025

How to Become an AI-Powered DevOps or Cloud Engineer in 2025

The technology landscape is rapidly evolving, and the fusion of artificial intelligence with DevOps and...
Read More