【发布时间】:2015-05-21 23:48:02
【问题描述】:
我正在使用 jdk1.8.0_40
我想制作一个可以动态更改列的 TableView,但它会在 Old/Tenured gen 中泄漏内存并最终挂起 JVM。
这是一个演示泄漏的简单程序(不包括导入):
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); }
}
在上面的代码中,第二次按下按钮应该清除所有项目和列,确实如此,但某些引用以某种方式在某处持续存在。
我已经尝试过的一些事情:
- 尝试搜索类似问题,但未找到任何相关内容。
- 尝试将所有引用存储在 ArrayList 中并显式删除()而不是 clear() - 没有奏效。
- 尝试使用 ListView,效果很好,但它不是表格,不能有列。
- 尝试使用不同的类组合(例如,使用 StringProperty 而不是 String),这也不足为奇。
我最近才开始学习 JavaFX,所以如果是这样的话,我可能只是遗漏了一些明显的东西 - 对不起一个愚蠢的问题。
如何修复此漏洞?
如果无法修复,这是一个错误,还是这是预期的行为,我不应该动态创建 TableColumn?
【问题讨论】:
标签: memory-leaks javafx tableview javafx-8 tablecolumn