22.15 Java Memory Management (Garbage Collection)

Java Memory Management (Garbage Collection)

Overview

Java manages memory automatically using garbage collection, which removes unused objects from memory.

Topics

Examples

public class Example {
    public static void main(String[] args) {
        Example obj = new Example();
        obj = null; // Eligible for garbage collection
        System.gc(); // Request garbage collection
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("Object is being garbage collected");
    }
}

Tags

#java #memorymanagement #garbagecollection