Garbage collection in Java is the automatic process by which the Java Virtual Machine (JVM) identifies and removes objects that are no longer reachable or in use by the program. The primary purpose of garbage collection is to reclaim memory occupied by objects that are no longer needed, freeing up resources and preventing memory leaks.
How Garbage Collection Works
Mark and Sweep: The JVM uses a “mark-and-sweep” algorithm. It starts by marking all objects that are reachable from the root (typically, objects referenced by the main method or active threads). Then, it sweeps through the heap, reclaiming memory occupied by objects not marked during the marking phase.
Generational Garbage Collection: The heap is often divided into young and old generations. New objects are initially allocated in the young generation, and a minor garbage collection is performed frequently in this space. Objects that survive several rounds of garbage collection in the young generation are promoted to the old generation, where a major garbage collection is less frequent.
The finalize() Method:The finalize() method is a method provided by the Object class in Java. It is called by the garbage collector before an object is reclaimed. However, it’s important to note that the use of finalize() is discouraged, and it’s considered best practice to use other mechanisms, such as the try-with-resources statement and the AutoCloseable interface, for resource management.
Here is the basic structure of the finalize() method:
protected void finalize() throws Throwable {
// Cleanup or resource release logic
super.finalize(); // Call the superclass finalize method
}
Code language: JavaScript (javascript)
Here’s an example that demonstrates how to use the Runnable interface:
class ResourceHandler { private String resourceName; public ResourceHandler(String resourceName) { this.resourceName = resourceName; } // Overriding the finalize() method for resource cleanup @Override protected void finalize() throws Throwable { try { // Release resources associated with the object System.out.println("Cleaning up resources for: " + resourceName); } finally { super.finalize(); } } } class FinalizeDemo{ public static void main(String[] args) { // Creating an object ResourceHandler resource = new ResourceHandler("ExampleResource"); // Making the object eligible for garbage collection resource = null; // Triggering garbage collection (explicitly calling is for demonstration purposes) System.gc(); } }
Output:
D:\>java FinalizeDemo
Cleaning up resources for: ExampleResource
In this example:
The finalize() method is overridden in the MyClass to clean up the resource.
Objects obj1 and obj2 are set to null to make them eligible for garbage collection.
System.gc() is used to request garbage collection, but it’s generally discouraged to explicitly call it due to its unpredictable nature.
While finalize() can be used for resource cleanup, it’s recommended to explicitly release resources using other means (like try-with-resources for external resources) rather than relying solely on finalize(). Garbage collection and finalize() serve as part of Java’s memory management, ensuring efficient use of memory resources.