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
- JVM Memory Model (Heap, Stack, Method Area)
- Garbage Collection Process
- Finalization
- Best Practices for Memory Management
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");
}
}