memory-leaksjavafxtableviewjavafx-8tablecolumn

JavaFX memory leak in TableView with dynamic TableColumns


I am using jdk1.8.0_40

I want to make a TableView which can have dynamically changing columns, but it leaks memory in Old/Tenured gen and eventually hangs the JVM.

Here is a simple program that demonstrates the leak (imports not included):

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {    
        TableView<ObservableList<String>> tableView = new TableView<>();
        Button button = new Button("button");

        button.setOnAction((actionEvent) -> {
            // Clearing items and/or columns here doesn't remove the leak
            tableView.getItems().clear();
            tableView.getColumns().clear();

            for (int g = 0; g < 50; g++) { // iterate 50 times to see the leak quickly
                TableColumn<ObservableList<String>, String> tableColumn = new TableColumn<>("column");
                tableView.getColumns().add(tableColumn);
            }
                tableView.getItems().add(FXCollections.observableArrayList("1"));

            // clearing the items here removes the leak somehow, but also makes the table useless
            /* tableView.getItems().clear(); */
        });

        primaryStage.setScene(new Scene(new VBox(tableView, button), 800, 600));
        primaryStage.show();
    }    
    public static void main(String[] args) { launch(args); }
}

In the code above pressing the button 2nd time should clear all items and columns, and it does, but some reference somehow persists somewhere.

Some things I've already tried:

I have only recently started learning JavaFX so I may just be missing something obvious, if so - sorry for a dumb question.

How do I fix this leak?

If it can't be fixed, is it a bug, or is this intended behavior and I should not create TableColumn dynamically?


Solution

  • To answer my own question in case somebody is also having this issue - this is a bug.

    I submitted a JIRA bug report, for more info and maybe updates see https://javafx-jira.kenai.com/browse/RT-40325