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

Memory Leak with nested Lambdas

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: hotspot
    • Labels:
      None

      Description

      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.

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved: