finalize()
Method in Java
The finalize()
method in Java is a method of the Object
class used for cleanup operations
before an object is removed from memory by the garbage collector. It provides an opportunity for an object to perform
any necessary cleanup (like releasing resources) before being destroyed.
Key Points About finalize()
- Defined in the
Object
Class: Every Java class inherits thefinalize()
method from theObject
class. Its default implementation does nothing. - Purpose: Typically used to release non-Java resources like file handles, database connections, or native code resources. Not recommended for general cleanup, as it can introduce unpredictable behavior.
- Syntax:
protected void finalize() throws Throwable { // Cleanup code here }
- Called by the Garbage Collector:
The garbage collector calls the
finalize()
method before reclaiming memory for the object. There is no guarantee when or even iffinalize()
will be called, as it depends on the garbage collector.
Example of finalize()
Usage
class MyClass {
// Constructor
MyClass() {
System.out.println("Object created.");
}
// Overriding finalize method
@Override
protected void finalize() throws Throwable {
System.out.println("finalize() method called.");
}
}
public class Test {
public static void main(String[] args) {
MyClass obj1 = new MyClass();
obj1 = null; // Eligible for garbage collection
// Requesting garbage collection
System.gc();
System.out.println("End of program.");
}
}
Output:
Object created. finalize() method called. End of program.
(Note: The actual output might vary as the finalize()
method's execution is dependent on the garbage collector.)
Important Considerations
- Unpredictability: The
finalize()
method is not guaranteed to be called immediately when an object is no longer referenced. It may never be called if the program ends before garbage collection. - Deprecated in Java 9 and Later: The
finalize()
method was deprecated starting with Java 9 due to its unpredictability and potential for misuse. Alternatives liketry-with-resources
or explicitly closing resources (close()
) are preferred. - Performance Impact: Relying on
finalize()
can lead to performance issues, as objects requiring finalization take longer to be garbage collected.
Alternatives to finalize()
try-with-resources
(Preferred): Use for managing resources like files and streams.try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) { String line = reader.readLine(); System.out.println(line); } catch (IOException e) { e.printStackTrace(); }
- Explicit Resource Cleanup:
Implement a method (e.g.,
close()
) for releasing resources.class Resource { void close() { System.out.println("Resource closed."); } } public class Test { public static void main(String[] args) { Resource res = new Resource(); res.close(); } }
Conclusion
While the finalize()
method was historically used for resource cleanup, it is now considered outdated and unreliable.
Use modern techniques like try-with-resources
or explicit cleanup methods for better performance, control, and predictability.