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

Memory Leak: MenuBar added to Tab is still referenced on tab close and is not garbage collected

    Details

    • Subcomponent:
    • Introduced In Version:
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_102"
      Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      MenuBar instances added to Tab in a TabPane are still referenced if the parent tab or tabpane is closed.

      Please note that in the provided test case, the Payload is only present to make it easier to see the memory leak in a profiler. The test case was derived from a real world issue, where the MenuBar referenced further Objects due to listeners (lambdas).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Open example application
      2. Attach jvisualvm and profile memory
      3. Press button in UI multiple times
      4. Close all open tabs
      4. Trigger GC
      5. Memory is not garbage collected, Heap dump shows MenuBar items are still referenced.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      MenuBar instance should be garbage collected when the parent tab is closed.
      ACTUAL -
      MenuBar items are still referenced and cannot be garbage collected. See error message below.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "JavaFX Application Thread" java.lang.OutOfMemoryError: Java heap space
      at MenuButtontest$Payload.<init>(MenuButtontest.java:49)
      at MenuButtontest$1.<init>(MenuButtontest.java:32)
      at MenuButtontest.lambda$0(MenuButtontest.java:30)
      at MenuButtontest$$Lambda$72/492183732.handle(Unknown Source)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.Node.fireEvent(Node.java:8411)
      at javafx.scene.control.Button.fire(Button.java:185)
      at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
      at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.Menu;
      import javafx.scene.control.MenuBar;
      import javafx.scene.control.Tab;
      import javafx.scene.control.TabPane;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;

      public class MenuButtonTest extends Application {

          public static void main(String[] args) {
              Application.launch(MenuButtonTest.class, args);
          }

          @Override
          public void start(Stage stage) {


              Button button = new Button("add tab with menu");
              BorderPane pane = new BorderPane(button);
              TabPane tabPane = new TabPane();

              button.setOnAction(event -> {

                  Tab tab = new Tab("foo");

                  Menu rootMenu = new Menu("some name");
                  MenuBar menuBar = new MenuBar(rootMenu) {

                      private Payload payload = new Payload();

                  };

                  tab.setContent(menuBar);
                  tabPane.getTabs().add(tab);
              });

              pane.setTop(tabPane);

              Scene scene = new Scene(pane);
              stage.setScene(scene);
              stage.show();
          }

          public static class Payload {

              private Object[] objects = new Object[10000000];

          }
      }
      ---------- END SOURCE ----------

        Attachments

        1. 8u102_profiler.xml
          1.03 MB
        2. 8u102.nps
          31 kB
        3. 8u112ea_profiler.xml
          1.03 MB
        4. 8u112ea.png
          8u112ea.png
          42 kB
        5. MenuButtonTest.java
          1 kB

          Issue Links

            Activity

              People

              • Assignee:
                jgiles Jonathan Giles
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: