Skip to content

Commit ec7cb6d

Browse files
author
Brent Christian
committedDec 8, 2021
8276447: Deprecate finalization-related methods for removal
Reviewed-by: rriggs, alanb, lancea, darcy, mchung, serb, smarks, prr
1 parent 3c2951f commit ec7cb6d

File tree

62 files changed

+156
-140
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+156
-140
lines changed
 

‎src/java.base/share/classes/java/lang/Enum.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,13 @@ public static <T extends Enum<T>> T valueOf(Class<T> enumClass,
275275

276276
/**
277277
* enum classes cannot have finalize methods.
278+
*
279+
* @deprecated Finalization has been deprecated for removal. See
280+
* {@link java.lang.Object#finalize} for background information and details
281+
* about migration options.
278282
*/
279-
@SuppressWarnings("deprecation")
283+
@Deprecated(since="18", forRemoval=true)
284+
@SuppressWarnings("removal")
280285
protected final void finalize() { }
281286

282287
/**

‎src/java.base/share/classes/java/lang/Object.java

+24-16
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,12 @@ public final void wait(long timeoutMillis, int nanos) throws InterruptedExceptio
478478
* A subclass overrides the {@code finalize} method to dispose of
479479
* system resources or to perform other cleanup.
480480
* <p>
481+
* <b>When running in a Java virtual machine in which finalization has been
482+
* disabled or removed, the garbage collector will never call
483+
* {@code finalize()}. In a Java virtual machine in which finalization is
484+
* enabled, the garbage collector might call {@code finalize} only after an
485+
* indefinite delay.</b>
486+
* <p>
481487
* The general contract of {@code finalize} is that it is invoked
482488
* if and when the Java virtual
483489
* machine has determined that there is no longer any
@@ -543,27 +549,29 @@ public final void wait(long timeoutMillis, int nanos) throws InterruptedExceptio
543549
* }
544550
* }</pre>
545551
*
546-
* @deprecated The finalization mechanism is inherently problematic.
547-
* Finalization can lead to performance issues, deadlocks, and hangs.
548-
* Errors in finalizers can lead to resource leaks; there is no way to cancel
549-
* finalization if it is no longer necessary; and no ordering is specified
550-
* among calls to {@code finalize} methods of different objects.
551-
* Furthermore, there are no guarantees regarding the timing of finalization.
552-
* The {@code finalize} method might be called on a finalizable object
553-
* only after an indefinite delay, if at all.
554-
*
555-
* Classes whose instances hold non-heap resources should provide a method
556-
* to enable explicit release of those resources, and they should also
557-
* implement {@link AutoCloseable} if appropriate.
558-
* The {@link java.lang.ref.Cleaner} and {@link java.lang.ref.PhantomReference}
559-
* provide more flexible and efficient ways to release resources when an object
560-
* becomes unreachable.
552+
* @deprecated Finalization is deprecated and subject to removal in a future
553+
* release. The use of finalization can lead to problems with security,
554+
* performance, and reliability.
555+
* See <a href="https://openjdk.java.net/jeps/421">JEP 421</a> for
556+
* discussion and alternatives.
557+
* <p>
558+
* Subclasses that override {@code finalize} to perform cleanup should use
559+
* alternative cleanup mechanisms and remove the {@code finalize} method.
560+
* Use {@link java.lang.ref.Cleaner} and
561+
* {@link java.lang.ref.PhantomReference} as safer ways to release resources
562+
* when an object becomes unreachable. Alternatively, add a {@code close}
563+
* method to explicitly release resources, and implement
564+
* {@code AutoCloseable} to enable use of the {@code try}-with-resources
565+
* statement.
566+
* <p>
567+
* This method will remain in place until finalizers have been removed from
568+
* most existing code.
561569
*
562570
* @throws Throwable the {@code Exception} raised by this method
563571
* @see java.lang.ref.WeakReference
564572
* @see java.lang.ref.PhantomReference
565573
* @jls 12.6 Finalization of Class Instances
566574
*/
567-
@Deprecated(since="9")
575+
@Deprecated(since="9", forRemoval=true)
568576
protected void finalize() throws Throwable { }
569577
}

0 commit comments

Comments
 (0)
Please sign in to comment.