Assignments/Article By Silvia Scano

Garbage Collection in Java

Student Assignment


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

Java Spring Boot

Java Spring Boot: A Comprehensive Introduction to Microservices

Introduction to Java Spring Boot and Microservices Java Spring Boot has emerged as an important...
Read More
Fundamentals of Scala Programming

Mastering the Fundamentals of Scala Programming: A Complete Guide to Functional Programming

Introduction to Scala Programming Scala programming has gained immense popularity in recent years thanks to...
Read More
Java Programming

Java Basics for Beginners: A Step-by-Step Guide to Mastering Java Programming

Introduction to Java programming Java, a versatile and extensively utilised programming language, is popular among...
Read More