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

Memory Leak in (Context)Menu

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: fx2.1
    • Fix Version/s: 8
    • Component/s: javafx

      Description

      Our use case: when a ContextMenu is shown, we load asynchronously some items and then populate the ContextMenu.

      During loading we show a fix MenuItem with a ProgressIndicator in it.
      There is also another fix SeperatorMenuItem.

      We figured out, that there are memory leaks, though. (Everytime we clear and add the menuitem).

      See also here:
      https://forums.oracle.com/forums/thread.jspa?threadID=2407497&tstart=0
      Quote: "I believe that the problem is related to the javafx.beans.InvalidationListener not cleaning up it's link to the javafx.scene.control.skin.ContextMenuContent$MenuItemContainer."

      Here is a simple test case.


      import javafx.application.Application;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ContextMenu;
      import javafx.scene.control.MenuItem;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;
      import javafx.stage.WindowEvent;
       
      public class MemoryLeakTest2 extends Application {
          public static void main(String[] args) {
              launch(args);
          }
       
          @Override
          public void start(Stage stage) throws Exception {
              VBox root = new VBox();
       
              final ContextMenu menu = new ContextMenu();
              Button button = new Button("Test");
              button.setContextMenu(menu);
              root.getChildren().add(button);
              final MenuItem menuItem1 = new MenuItem("Test");
       
              menu.getItems().add(menuItem1);
       
              menu.setOnShown(new EventHandler<WindowEvent>() {
                  @Override
                  public void handle(WindowEvent windowEvent) {
                      for (int i = 0; i < 100; i++) {
                          menu.getItems().clear();
                          menu.getItems().add(menuItem1);
                      }
                      System.gc();
                      System.out.println((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 + " KB");
                  }
              });
       
       
              Scene scene = new Scene(root);
              stage.setScene(scene);
              stage.show();
          }
      }

        Attachments

          Activity

            People

            • Assignee:
              jgiles Jonathan Giles
              Reporter:
              cschudtjfx Christian Schudt (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Due:
                Created:
                Updated:
                Resolved:
                Imported: