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

Memory Leak in TreeView

XMLWordPrintable

      We've discovered memory leaks in the TreeView.

      Run the code, click the button several times and watch the memory usage go up very quickly.

      I recognized, that treeView.layout() accelerates the effect, but you can see it, too, if you remove it (you just have to click more often ;-) )

      What I do is, I clear all children of the root node, and add new children every time.

      I guess this issue is related to RT-20616 and RT-16529.

      If you can suggest any workaround, while this hasn't been fixed, it would be fine great!

      How likely is that the memory leaks issues will get fixed in 2.2?
      Our application becomes unusable very quickly due to this bug.




      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.TreeItem;
      import javafx.scene.control.TreeView;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class MemoryLeakTest extends Application {

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

          @Override
          public void start(Stage stage) throws Exception {
              VBox root = new VBox();

              final TreeItem<String> rootItem = new TreeItem<String>();
              final TreeView<String> treeView = new TreeView<String>(rootItem);
              treeView.setShowRoot(false);

              root.getChildren().add(treeView);

              Button button = new Button("clear + add");
              button.setOnAction(new EventHandler<ActionEvent>() {
                  @Override
                  public void handle(ActionEvent actionEvent) {

                      for (int j = 0; j < 50; j++) {
                          treeView.getRoot().getChildren().clear();
                          for (int i = 0; i < 500; i++) {
                              TreeItem<String> item = new TreeItem<String>();
                              item.setValue(Integer.toString(i));
                              treeView.getRoot().getChildren().add(item);
                          }
                          treeView.layout();
                      }
                      long totalMemory = Runtime.getRuntime().totalMemory() / 1024;
                      long freeMemory = Runtime.getRuntime().freeMemory() / 1024;
                      System.out.println("Heap use: " + (totalMemory - freeMemory) + " / " + totalMemory + " kB");
                  }
              });
              root.getChildren().add(button);

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

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

              Created:
              Updated:
              Resolved:
              Imported: