Weak handles leak causes GC to take longer

Denghui Dong denghui.ddh at alibaba-inc.com
Thu Apr 1 15:47:52 UTC 2021


Hi,

One of our applications using JDK8 has the problem of slow increase in YGC time-consuming,
and I found that the problem is caused by the weak handles leak.

The following code can reproduce this problem after running for a long time.

```
class Test {
    static MethodHandles.Lookup lookup = MethodHandles.lookup();
    public static void main(String[] args) {
      while (true) {
            MethodType type = MethodType.methodType(double.class, double.class);
            try {
                MethodHandle mh = lookup.findStatic(Math.class, "log", type);
                System.err.println(mh);
            } catch (Throwable t) {
            }
      }
    }
}
```

I think it's a known problem, there are two issues related to this problem,
JDK-8152271: MemberNameTable doesn't purge stale entries
JDK-8174749: Use hash table/oops for MemberName table

JDK-8152271 already exists in 8u but doesn't resolve this problem.
So I tried to backport JDK-8152271 and another performance improvement issue JDK-8249719,
it works, but introduced another performance problem, and also the backport is not clean.

So i tried another way, and I think the following fix may rolve the problem,

diff --git a/src/share/vm/prims/methodHandles.cpp b/src/share/vm/prims/methodHandles.cpp
index 9e19dbbe3..fb9d0be7e 100644
--- a/src/share/vm/prims/methodHandles.cpp
+++ b/src/share/vm/prims/methodHandles.cpp
@@ -972,6 +972,13 @@ oop MemberNameTable::find_or_add_member_name(jweak mem_name_wref) {
       return mname;
     }
   }
+
+  if (new_index < len) {
+    assert(JNIHandles::resolve(this->at(new_index)) == NULL, "sanity");
+    // destory the old handle
+    JNIHandles::destroy_weak_global(this->at(new_index));
+  }
+
   // Not found, push the new one, or reuse empty slot
   this->at_put_grow(new_index, mem_name_wref);
   return new_mem_name;


I didn't file a new issue in JBS yet.
I want to hear your suggestions because I am not familiar with the implementation of MemberName-related stuff,
so I am not sure if there is a problem with this fix.

testing: linux_x86 jdk/test/java/lang/invoke

Any input is appreciated.

Thanks,
Denghui


More information about the jdk8u-dev mailing list