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

Memory leak on replacing selection/focusModel

    XMLWordPrintable

Details

    Description

      Initially detected/reported against selectionModel of ChoiceBox, similar issue for other controls with their respective selection/focusModels (for details, see comment below).

      Following failing test case for ChoiceBox. Reason seems to be that ChoiceBoxSelectionModel registers strong listeners to both itemsProperty and the content of the items. Replacing both with weakListeners lets the test pass.

      other controls with a selectionModel might have a similar issue

      Failing test (as usual, test for leak c&p'ed from available tests ... ):

          private WeakReference<SingleSelectionModel<?>> weakSmRef;
          /**
           * Memory leak on replacing selectionModel.
           */
          @Test
          public void testReplaceSelectionModelMemory() {
              weakSmRef = new WeakReference<>(box.getSelectionModel());
              SingleSelectionModel<String> replacingSm = ChoiceBoxShim.get_ChoiceBoxSelectionModel(box);
              box.setSelectionModel(replacingSm);
              attemptGC(10);
              assertNull("selectionModel must be gc'ed", weakSmRef.get());
          }
          
          private void attemptGC(int n) {
              // Attempt gc n times
              for (int i = 0; i < n; i++) {
                  System.gc();
                  System.runFinalization();

                  if (weakSmRef.get() == null) {
                      break;
                  }
                  try {
                      Thread.sleep(500);
                  } catch (InterruptedException e) {
                     System.err.println("InterruptedException occurred during Thread.sleep()");
                  }
              }
          }
          

      Attachments

        Issue Links

          Activity

            People

              fastegal Jeanette Winzenburg
              fastegal Jeanette Winzenburg
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: