Skip to content

Commit aeef909

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Make ClosingFuture use PhantomReference instead of finalize().
Compare what 7c6b17c did for `FileBackedOutputStream`. As in that CL, this CL adds `reachabilityFence` calls. I think that the calls are technically more necessary here than they were in `FileBackedOutputStream`, where we could probably [rely on `synchronized` to keep the object alive](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/ref/Reference.html#reachabilityFence(java.lang.Object)). Incidentally, they are equally necessary under the current `ClosingFuture` approach that uses `finalize()`. (So that gives some indication of how much they matter _in practice_: probably not much.) I had a thought at one point that maybe `reachabilityFence` was necessary only in the _terminal_ operations: Since every `ClosingFuture` pipeline should end with a terminal operation, we should be able to rely on a `reachabilityFence` in that terminal operation to keep the chain alive. However, I now think it's best to put a fence in all operations: - It's simpler. - It behaves better for broken code that lacks a terminal operation: If not for fences on intermediate operations, then such code might see its `ClosingFuture` closed by GC while it's still operating on it. And while such code is buggy, I don't think we want to cause its intermediate operations to _throw_. By putting `reachabilityFence` calls in such operations, we keep the `ClosingFuture` alive until those operations complete, at which point it can leak "properly," getting closed with no harm to anyone except the resulting log message. Finally: As a minor follow-up to 9744861, I arranged for the `Finalizer` thread to always have an unobfuscated _thread name_, even when the class name of `Finalizer` gets obfuscated. RELNOTES=Migrated some prod classes from `finalize()` to `PhantomReference` in preparation for [the removal of finalization](https://openjdk.org/jeps/421). (7c6b17c) PiperOrigin-RevId: 881753103
1 parent dae9566 commit aeef909

4 files changed

Lines changed: 556 additions & 248 deletions

File tree

‎android/guava/src/com/google/common/base/internal/Finalizer.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static void startFinalizer(
7272
*/
7373

7474
Finalizer finalizer = new Finalizer(finalizableReferenceClass, queue, frqReference);
75-
String threadName = Finalizer.class.getName();
75+
String threadName = "com.google.common.base.internal.Finalizer";
7676
Thread thread = null;
7777
if (bigThreadConstructor != null) {
7878
try {

0 commit comments

Comments
 (0)