JavaFX: cleaning up tableview instances from memory
How should I clean up the tableview instance from memory,
From what I learned,
>Delete all listeners attached to the table (does it apply to the columns and rows of the corresponding table?) > Clear all columns And > have no strong references to table instances
From the external reference, I did the following,
>Set the focus model to null. > Set the mouse button listener to null. > Setselectionmodel is null. > Observablearraylist. With setitems null
Finally, my code is as follows
//I gave a try for,RowFactory and ColumnFactory to null. tableView.setRowFactory(null); for (TableColumn column : this.tableView.getColumns()) { column.setCellFactory(null); column.setCellValueFactory(null); } tableView.getFocusModel().focus(null); tableView.setOnMouseClicked(null); tableView.setSelectionModel(null); tableView.getColumns().clear(); tableView.setItems(FXCollections.observableArrayList()); tableView = null;
My question is:
I open multiple table views. When I close, (the hashmapnode (from the profiler) related to the profileview is still in memory and not published). Therefore, whenever I close the tableview, I will call the above code
In addition, I use the internal class to set the following setcellfactory,
column.setCellFactory((TableColumn<?,?> param) -> new EditingTableCell()); private class EditingTableCell extends TableCell<?,?> { //..... }
So, how should I properly clean up a tableview instance so that it can be garbage collected
Solution
3 (no strong reference to table instances) should be sufficient
If there is no stronger reference, the listener is a weak reference and does not prevent the GC from cleaning up the object
However, if the tableview is included in a tab, it may not be a GC when closing the tab, because JavaFX keeps a strong reference to the last closed tab inside it (it took me some time to find out - I consider this an unwelcome bug like behavior)