DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java OOPTopic 44 of 59
←PreviousPrevNextNext→

Java Object Finalization and Cleanup

Introduction to Object Cleanup in Java

In Java, memory management is handled automatically by the garbage collector, which reclaims memory from objects that are no longer in use. This reduces the need for manual memory handling and helps prevent common programming errors such as memory leaks and dangling pointers.

While memory is managed automatically, developers are still responsible for releasing external resources like file handles, sockets, or database connections. Java provides mechanisms such as the `AutoCloseable` interface, try-with-resources, and the `Cleaner` API to ensure proper resource cleanup.

Garbage Collection Overview

- Java automatically reclaims memory when objects are no longer reachable

- The garbage collector runs periodically and its timing cannot be precisely controlled

- There's no guarantee when (or if) `finalize()` will run

- For important resource release, explicit cleanup methods are recommended

The finalize() Method (Deprecated)

The `finalize()` method was once used to release resources before an object was collected, but it is now deprecated because it is unpredictable, can cause performance issues, and may lead to resource leaks.

Example
public class ResourceHolder {
    private String resource;
    
    public ResourceHolder(String res) {
        this.resource = res;
        System.out.println("Resource acquired: " + resource);
    }
    
    // Deprecated - avoid using in new code
    @Override
    protected void finalize() throws Throwable {
        try {
            System.out.println("Finalizing: " + resource);
            // Cleanup code here
        } finally {
            super.finalize();
        }
    }
    
    public void close() {
        System.out.println("Explicitly closing: " + resource);
    }
    
    public static void main(String[] args) {
        ResourceHolder rh = new ResourceHolder("FileHandle");
        rh.close(); // Prefer explicit cleanup
        rh = null; // Eligible for GC
        System.gc(); // Hint to GC (not guaranteed)
    }
}
Output
Resource acquired: FileHandle
Explicitly closing: FileHandle

AutoCloseable and try-with-resources

The preferred way to manage resources in Java is implementing the `AutoCloseable` interface and using try-with-resources, which ensures deterministic cleanup when leaving the block.

Example
public class FileResource implements AutoCloseable {
    private String filename;
    
    public FileResource(String filename) {
        this.filename = filename;
        System.out.println("Opening file: " + filename);
    }
    
    public void readData() {
        System.out.println("Reading from: " + filename);
    }
    
    @Override
    public void close() {
        System.out.println("Closing file: " + filename);
    }
    
    public static void main(String[] args) {
        try (FileResource file = new FileResource("data.txt")) {
            file.readData();
        }
        System.out.println("Resource cleanup completed");
    }
}
Output
Opening file: data.txt
Reading from: data.txt
Closing file: data.txt
Resource cleanup completed

Cleaner API (Java 9+)

The `Cleaner` API, introduced in Java 9, provides a safer and more reliable alternative to `finalize()`. It allows registering cleanup tasks that run after an object becomes unreachable.

Example
import java.lang.ref.Cleaner;

public class CleanerExample {
    private static final Cleaner cleaner = Cleaner.create();
    
    private final String resourceName;
    private final Cleaner.Cleanable cleanable;
    
    private static class ResourceCleaner implements Runnable {
        private final String resourceName;
        ResourceCleaner(String resourceName) {
            this.resourceName = resourceName;
        }
        @Override
        public void run() {
            System.out.println("Cleaning up: " + resourceName);
        }
    }
    
    public CleanerExample(String resourceName) {
        this.resourceName = resourceName;
        this.cleanable = cleaner.register(this, new ResourceCleaner(resourceName));
        System.out.println("Created: " + resourceName);
    }
    
    public void useResource() {
        System.out.println("Using: " + resourceName);
    }
    
    public static void main(String[] args) {
        CleanerExample example = new CleanerExample("NetworkConnection");
        example.useResource();
        example = null; 
        System.gc();
        try { Thread.sleep(1000); } catch (InterruptedException e) {}
    }
}
Output
Created: NetworkConnection
Using: NetworkConnection
Cleaning up: NetworkConnection

Phantom References for Cleanup

Phantom references, combined with a `ReferenceQueue`, allow fine-grained post-mortem cleanup control. They are an advanced alternative to `finalize()` for special cases where more control is needed.

Example
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

public class PhantomReferenceExample {
    private static final ReferenceQueue<Object> queue = new ReferenceQueue<>();
    
    public static void main(String[] args) {
        Object resource = new Object();
        PhantomReference<Object> phantomRef = new PhantomReference<>(resource, queue);
        
        System.out.println("Resource created");
        resource = null; 
        System.gc();
        
        try {
            PhantomReference<?> cleaned = (PhantomReference<?>) queue.remove(1000);
            if (cleaned != null) {
                System.out.println("Resource was garbage collected");
                // Perform custom cleanup here
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Output
Resource created
Resource was garbage collected

Best Practices for Resource Management

- Use try-with-resources for predictable cleanup

- Implement AutoCloseable for classes managing external resources

- Prefer the Cleaner API (Java 9+) over finalize()

- Do not rely on finalize() as it is deprecated and unreliable

- Explicitly close resources as soon as possible

- Use finally blocks when try-with-resources is not feasible

- PhantomReference is suitable only for advanced cases

Comparison Table

ApproachWhen to UseReliabilityPerformance
try-with-resourcesDeterministic cleanupHighExcellent
Cleaner APIAutomatic non-memory cleanupHighGood
finalize()Legacy code onlyLowPoor
PhantomReferenceSpecialized advanced cleanupMediumGood
Test your knowledge: Java Object Finalization and Cleanup
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java OOPTopic 44 of 59
←PreviousPrevNextNext→