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

Possible memory leak from context menu

XMLWordPrintable

    • x86_64
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      This happens on:
      Java 1.8 update 131 32bit Linux (redhat 6.8)
      Java 1.8 update 172 64bit Windows 10
      Java 1.8 update 162 64bit Windows 10


      A DESCRIPTION OF THE PROBLEM :
      Adding and removing CustomMenuItems from MenuButton and ContextMenu seems to leak memory.
      Running JVisualVM and comparing the heap differences shows an ever increasing number of CustomMenuItems, ContextMenuContext$MenuItemContainer objects

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the test program in this bug report then open JVisualVM.
      Take heap dump and wait for 5mins or more before taking another heap dump; then compare the heap dumps, filtering for Javafx.scene.control

      Further heap dumps and comparisons will show an ever increasing number of CustomMenuItem and ContextMenuContentXXX objects.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected result is number of CustomMenuItem does not increase as all objects added to the menu is removed.
      ACTUAL -
      Number of CustomMenuItem objects constantly increase.

      ---------- BEGIN SOURCE ----------
      package application;

      import java.util.Vector;

      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.geometry.Side;
      import javafx.stage.Stage;
      import javafx.scene.Scene;
      import javafx.scene.control.MenuButton;
      import javafx.scene.layout.HBox;


      public class Main extends Application {

      public static MenuButton m_Menu = new MenuButton("Menu");

      @Override
      public void start(Stage primaryStage) {
      try {
      HBox root = new HBox();
      Scene scene = new Scene(root,400,400);
      m_Menu.setPopupSide(Side.BOTTOM);
      root.getChildren().addAll(m_Menu);
      primaryStage.setScene(scene);
      primaryStage.show();

      //create thread to simulate dynamic adding and
      //removing from the menu
      Thread t = new Thread(() -> {
      dynamicMenuThread();
      });
      t.start();
      } catch(Exception e) {
      e.printStackTrace();
      }
      }

      public void dynamicMenuThread() {
      Vector<TestMenuItem> list = new Vector<TestMenuItem>();
      while(true) {
      //create items
      Platform.runLater(() -> {
      for(int i = 1; i < 6; ++i) { //make menu items
      TestMenuItem item = new TestMenuItem(m_Menu.getItems());
      item.addToMenu(i);
      item.setItem("Item"+i, i);
      list.add(item);
      }
      for(int i = 6; i < 11; ++i) { //make menu items with sub items
      TestMenuItem item = new TestMenuItem(m_Menu.getItems());
      item.addToMenu(i);
      item.setItem("Item"+i, i);
      TestMenuItem subItem = item.addSubItem();
      subItem.setItem("subItem1", 1);
      subItem.addToMenu(0);
      list.add(subItem);
      subItem = item.addSubItem();
      subItem.setItem("subItem2", 2);
      subItem.addToMenu(1);
      list.add(subItem);
      list.add(item);
      }
      }
      );
      try {
      Thread.sleep(1000);
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      //remove items
      while(true) {
      if(list.isEmpty()) {
      break;
      }
      Platform.runLater(() -> {
      //remove some items
      list.remove(0).removeFromMenu();
      });
      try {
      Thread.sleep(500);
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      }
      }
      }

      public static void main(String[] args) {
      launch(args);
      }
      }

      --------- END SOURCE ----------

      FREQUENCY : always


            aghaisas Ajit Ghaisas
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: