From 1619e47ba95c9665d42b3788f592c50bad371d66 Mon Sep 17 00:00:00 2001 From: Devin Papineau Date: Thu, 14 Mar 2024 19:22:05 -0400 Subject: [PATCH] Add MemberName finalizer to mark clazz for MemberName list pruning This prevents a memory leak in the maintenance of per-class lists used to find affected MemberName objects in case of class redefinition. --- .../share/classes/java/lang/invoke/MemberName.java | 13 +++++++++++++ .../java/lang/invoke/MethodHandleNatives.java | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/java.base/share/classes/java/lang/invoke/MemberName.java b/src/java.base/share/classes/java/lang/invoke/MemberName.java index 3a979ce50b7..edf6f8f8af7 100644 --- a/src/java.base/share/classes/java/lang/invoke/MemberName.java +++ b/src/java.base/share/classes/java/lang/invoke/MemberName.java @@ -23,6 +23,12 @@ * questions. */ +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved + * =========================================================================== + */ + package java.lang.invoke; import sun.invoke.util.BytecodeDescriptor; @@ -1196,4 +1202,11 @@ private static MemberName[] newMemberBuffer(int length) { return buf; } } + + @Override + protected void finalize() { + if (null != clazz) { + MethodHandleNatives.markClassForMemberNamePruning(clazz); + } + } } diff --git a/src/java.base/share/classes/java/lang/invoke/MethodHandleNatives.java b/src/java.base/share/classes/java/lang/invoke/MethodHandleNatives.java index dd0a2417e19..3c8fd411214 100644 --- a/src/java.base/share/classes/java/lang/invoke/MethodHandleNatives.java +++ b/src/java.base/share/classes/java/lang/invoke/MethodHandleNatives.java @@ -23,6 +23,12 @@ * questions. */ +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2024, 2024 All Rights Reserved + * =========================================================================== + */ + package java.lang.invoke; import jdk.internal.access.JavaLangAccess; @@ -690,4 +696,9 @@ static Object classData(Class c) { UNSAFE.ensureClassInitialized(c); return JLA.classData(c); } + + /** + * Inform the VM that a MemberName belonging to class c has been collected. + */ + static native void markClassForMemberNamePruning(Class c); }