Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8352622

ClassValueMap cache retains associated value when ClassValue is garbage collected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 25
    • core-libs
    • None

      The Entry in the cache array of a ClassValueMap keeps a value in an outdated association alive when the ClassValue is garbage collected, which is technically a resource leak.

      See this test:

          @Test
          void testWeakAgainstClassValue() {
              ClassValue<int[]> cv = new ClassValue<>() {
                  @Override
                  protected int[] computeValue(Class<?> type) {
                      return new int[23];
                  }
              };

              WeakReference<?> ref = new WeakReference<>(cv.get(int.class));
              cv = null; // Remove reference for interpreter
              if (!ForceGC.wait(() -> ref.refersTo(null))) {
                  fail("Timeout");
              }
          }

            liach Chen Liang
            liach Chen Liang
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: