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

Additional Memory Leak in ControlAcceleratorSupport

XMLWordPrintable

    • x86_64
    • windows_10

        ADDITIONAL SYSTEM INFORMATION :
        Windows 10 1909 64bit
        OpenJDK 17 (build 17+35-2724)

        A DESCRIPTION OF THE PROBLEM :
        If there are MenuItems in the Stage, after closing the Stage, the MenuItems won't get garbage collected, and thus also leaking its listeners and other related objects. It only happens on JFX 17, on JFX 16 the memory leak won't happen. Might be related with https://github.com/openjdk/jfx/pull/429.

        REGRESSION : Last worked in version openjfx16

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Place a MenuItem in a Stage and launch this Stage, then launch a new Stage and close previous Stage, the MenuItem in the previous Stage won't get garbage collected.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The MenuItem in the closed Stage get garbage collected.
        ACTUAL -
        The MenuItem in the closed Stage won't be garbage collected.

        ---------- BEGIN SOURCE ----------
        import javafx.application.Application;
        import javafx.geometry.Pos;
        import javafx.scene.Scene;
        import javafx.scene.control.Button;
        import javafx.scene.control.Menu;
        import javafx.scene.control.MenuBar;
        import javafx.scene.control.MenuItem;
        import javafx.scene.layout.StackPane;
        import javafx.stage.Stage;
        import java.lang.ref.WeakReference;
        import java.util.ArrayList;
        import java.util.Arrays;

        public class JavaApp extends Application {

            private static final ArrayList<WeakReference<MenuItem>> uncollectedMenuItems = new ArrayList<>();

            private static void clearNullMenuItems() {
                uncollectedMenuItems.removeIf(ref -> ref.get() == null);
            }

            private static void launchNewStage(Stage stage) {
                MenuItem menuItem = new MenuItem("Restart Stage");
                menuItem.setOnAction(actionEvent -> {
                    launchNewStage(new Stage());
                    stage.close();
                });
                uncollectedMenuItems.add(new WeakReference<>(menuItem));
                MenuBar menuBar = new MenuBar(new Menu("MENU", null, menuItem));
                Button button = new Button("Call GC and Print MenuItems");
                button.setOnAction(actionEvent -> {
                    System.gc();
                    clearNullMenuItems();
                    System.out.println(Arrays.toString(uncollectedMenuItems.toArray()));
                });
                StackPane root = new StackPane(menuBar, button);
                StackPane.setAlignment(menuBar, Pos.TOP_CENTER);
                stage.setTitle("JFX-Test");
                stage.setMinWidth(400.0);
                stage.setMinHeight(300.0);
                stage.setScene(new Scene(root));
                stage.show();
            }

            @Override
            public void start(Stage primaryStage) {
                launchNewStage(primaryStage);
            }

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

        FREQUENCY : always


          1. App.java
            2 kB
          2. Capture.PNG
            Capture.PNG
            133 kB

              fkirmaier Florian Kirmaier
              webbuggrp Webbug Group
              Votes:
              2 Vote for this issue
              Watchers:
              10 Start watching this issue

                Created:
                Updated:
                Resolved: