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

Memory Leak with nested Lambdas

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • None
    • hotspot
    • None

      The code to reproduce is really simple and the test should be easy to understand.
      The behaviour is very counterintuitive.

      I've written a library to write tests for memory leaks, which makes this very easy to read and understand.
      The framework: https://github.com/Sandec/JMemoryBuddy

      This is the test:
          @Test
          public void lambdaSanityTest() {
              JMemoryBuddy.memoryTest(checker -> {
                  //fails
                  Runnable r = () -> { System.out.println("a");};

                  // works
                  //Runnable r = new Runnable() {
                  // @Override
                  // public void run() {
                  // System.out.println("b");
                  // }
                  //};
                  checker.assertCollectable(r); // not referenced should be collectable
              });
          }


      It seems as the lifetime of the runnable is the same as the lambda provided to the test.
      This also seems to be clearly wrong to me, because converting to an anonymous class fixes the problem.
      In my opinion, converting to anonymous classes shouldn't have any change in behavior.

      This might be a bug in Java Language, or in the hotspot.

            Unassigned Unassigned
            fkirmaier Florian Kirmaier
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: