2

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:

  • Tried searching for similar issues, but didn't find anything relevant.
  • Tried storing all the references in an ArrayList and explicitly remove()-ing them instead of clear() - didn't work.
  • Tried using ListView, which works fine, but it isn't a table and can't have columns.
  • Tried using different combinations of classes (e.g. StringProperty instead of String), which to no surprise also didn't work.

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?

2 Answers 2

2

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

0

If you suspect that there's a leak, you fix it by first identifying it using a profiler.

I rewrote your sample (which wasn't showing any data btw), there doesn't seem to be a leak. Try this and check the console output.

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class MainLeak extends Application {

    private static final int iterations = 100;
    private static final int columns = 50;
    private static final int rows = 100;

    @Override
    public void start(Stage primaryStage) {

        TableView<ObservableList<StringProperty>> table = new TableView<>();

        Button button = new Button("button");

        button.setOnAction((actionEvent) -> {

            addData( table, iterations);


        });

        addData( table, iterations);

        primaryStage.setScene(new Scene(new VBox(table, button), 800, 600));
        primaryStage.show();
    }

    private void addData( TableView table, int iterations) {

        for (int i = 0; i < iterations; i++) {

            final int index = i;

            addData(table);

            System.gc();

            System.out.println(index + ": free: " + Runtime.getRuntime().freeMemory() / 1024 + " kb, max: " + Runtime.getRuntime().maxMemory() / 1024 + " kb");

        }

    }

    private void addData(TableView table) {

        table.getItems().clear();
        table.getColumns().clear();

        for( int col=0; col < columns; col++) {
            table.getColumns().add(createColumn(col));
        }

        for( int row=0; row < rows; row++) {
            ObservableList<StringProperty> data = FXCollections.observableArrayList();
            for( int col=0; col < columns; col++) {
                data.add(new SimpleStringProperty(String.valueOf( row + " / " + col)));
            }
            table.getItems().add(data);
        }
    }

    private TableColumn<ObservableList<StringProperty>, String> createColumn(final int columnIndex) {

        TableColumn<ObservableList<StringProperty>, String> column = new TableColumn<>();
        String title = "Column " + columnIndex;
        column.setText(title);
        column.setCellValueFactory(cellDataFeatures -> cellDataFeatures.getValue().get(columnIndex));

        return column;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
2
  • None. Forgot to remove it. Thanks for the hint.
    – Roland
    Mar 19, 2015 at 8:57
  • Thanks for the reply. My code doesn't show any data because cell factory doesn't affect the leak. I ran your code and it steadily leaks memory in old gen memory after every button press. I'm using JProfiler to find the leak, here's a screenshot that shows the old gen increasing: i.imgur.com/NJKQpMB.png
    – Kirill
    Mar 19, 2015 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.