Member-only story

The finalize() Method is Deprecated: Demystifying Java’s Garbage Collection

Sanjay Singh
3 min readFeb 11, 2025

--

Introduction

🚨 Did you know that Java’s finalize() method has been deprecated since Java 9 and removed in Java 18? If you’re still relying on finalize() for resource clean-up, it’s time to rethink your approach.

This guide will explore: ✅ Why finalize() was deprecated and its problems.
✅ How Java’s Garbage Collector (GC) works.
✅ The right way to handle resource cleanup in modern Java.

The finalize() Method is Deprecated: Demystifying Java’s Garbage Collection

Story List Categories:

  • About Me & List of Stories
  • Java — All things Java-related.
  • Java Interview Playbook: Your Go-To Reading List — For interview preparation.
  • JAVA-8 — Dedicated to Java 8 topics.
  • Spring Boot & Spring — Focused on Spring and Spring Boot.
  • Microservices Topics List — Covering various microservices to

1. What is the finalize() Method?

Before Java 9, the finalize() method was used to perform cleanup before an object was garbage collected:

@Override
protected void finalize() throws Throwable {
System.out.println("Cleaning up before GC");
}

--

--

Sanjay Singh
Sanjay Singh

Written by Sanjay Singh

Java, Spring Boot & Microservices developer Sharing knowledge, tutorials & coding tips on my Medium page. Follow me for insights & see story list section

Responses (1)