I am experiencing memory leaks with TreeTableView when TableMenu is enabled. Steps to reproduce is simple. I have done some research why it's happening but will leave it to experts here to throw some light.
Run the embedded code with -Xmx 256m to reduce wait time. I am running with JDK 1.8.0_121
Both steps must be performed in sequence.
- Click on the Table Menu Menu Button the right top corner (+) just once no need to select any menu item.
Then Click (X) on the DYNAMIC column (Give it few secs to OOM)
package hellofx; import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeTableCell; import javafx.scene.control.TreeTableColumn; import javafx.scene.control.TreeTableView; import javafx.stage.Stage; public class TreeTableMemoryLeak extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { TreeTableView<Object> otable = buildTableView(); Platform.runLater(()->{ Scene scene = new Scene(otable,otable.getWidth(),otable.getHeight()); stage.setScene(scene); stage.show(); }); } private TreeTableView<Object> buildTableView() { TreeTableView<Object> table = new TreeTableView<>(); TreeTableColumn<Object, String> indexCol = new TreeTableColumn<>("Index"); indexCol.setCellFactory(column -> { return new TreeTableCell<Object, String>() { @Override protected void updateItem(String item, boolean empty) { setText(String.valueOf(getIndex())); } }; }); TreeTableColumn<Object, String> dynCol = new TreeTableColumn<>("DYNAMIC"); dynCol.setGraphic(new Button("X"){ { //ATTENTION 3 (On click of X remove all columns except first column) setOnAction((e)->dynCol.getColumns().removeIf(c->c!=dynCol.getColumns().get(0))); } }); //ATTENTION 1 table.setTableMenuButtonVisible(true); //ATTENTION 2 (39 is the magic number anything higher it will OOM on my device I have total 6G physical.) for(int i=0;i<39;i++){ dynCol.getColumns().add(new TreeTableColumn<>(String.valueOf(i))); } table.getColumns().addAll(indexCol,dynCol); table.setEditable(true); table.setRoot(new TreeItem<Object>()); return table; } }