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

Memory Leak in TabPane

XMLWordPrintable

      From this thread:
      https://forums.oracle.com/forums/post!reply.jspa?messageID=10631320

      In my opinion it is a bug in TabPane.

      Sample Application: Add and remove tabs and watch the memory increase.



      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.geometry.Side;
      import javafx.scene.Group;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.Tab;
      import javafx.scene.control.TabPane;
      import javafx.scene.layout.BorderPane;
      import javafx.scene.layout.HBox;
      import javafx.stage.Stage;

      public class TestApp4 extends Application {

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

          @Override
          public void start(Stage stage) throws Exception {
              stage.setTitle("Tabs example");

              // create window holder
              final TabPane tabPane = new TabPane();
              tabPane.setSide(Side.BOTTOM);
              tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.SELECTED_TAB);
              tabPane.getTabs().add(new MyTab("First Tab"));

              // create buttons
              Button add = new Button("Add Tab");
              Button remove = new Button("Remove Tab");


              HBox hBox = new HBox(8);
              hBox.getChildren().add(add);
              hBox.getChildren().add(remove);

              add.setOnAction(new EventHandler<ActionEvent>() {

                  @Override
                  public void handle(ActionEvent t) {
                      tabPane.getTabs().add(new MyTab("New Tab"));
                      System.gc();
                      System.out.println((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 + " KB");
                  }
              });

              remove.setOnAction(new EventHandler<ActionEvent>() {

                  @Override
                  public void handle(ActionEvent t) {
                      if (!tabPane.getSelectionModel().isEmpty()) {
                          tabPane.getTabs().remove(tabPane.getSelectionModel().getSelectedIndex());
                          System.gc();
                          System.out.println((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 + " KB");
                      }
                  }
              });


              BorderPane borderPane = new BorderPane();
              borderPane.setCenter(tabPane);
              borderPane.setTop(hBox);

              // add components to screen
              Group root = new Group();
              Scene scene = new Scene(root);

              borderPane.setPrefSize(500, 500);

              root.getChildren().add(borderPane);
              stage.setScene(scene);
              stage.centerOnScreen();
              stage.show();
          }

          class MyTab extends Tab {

              public MyTab(String title) {
                  super(title);
              }
          }
      }

            jgiles Jonathan Giles
            cschudtjfx Christian Schudt (Inactive)
            Votes:
            4 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: