JavaIntermediate#gc#memory

What is the purpose of the finalize() method and why is it discouraged?

finalize() was meant to run cleanup logic before an object is garbage collected, but it's unpredictable (no guaranteed timing), can hurt performance, and is deprecated since Java 9 in favor of try-with-resources and Cleaner.

Example
// Deprecated approach:
protected void finalize() { /* cleanup */ }
// Preferred: implement AutoCloseable and use try-with-resources

Related Questions

1
JavaIntermediate#serialization

What is serialization in Java?

Open
2
JavaIntermediate#serialization

What is the purpose of serialVersionUID?

Open
3
JavaIntermediate#serialization

What happens to static and transient fields during serialization?

Open