简体   繁体   中英

JavaFX : Clean up a TableView instance from Memory

How should i clean up a TableView instance from memory,

From what i learnt,

  1. Remove all listeners attached to the table. (is it applicable even to the respective table's columns and rows?)
  2. Clear all columns. and
  3. No strong reference to the table instance.

From an external reference i did the following,

  1. set focus model to null.
  2. set mouse,key listeners to null.
  3. setSelectionModel to null.
  4. setItems to an empty ObservableArraylist .

Finally, My code looks something like below.

 //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 problem is:

I have multiple table views opened and when i close, ( HashMapNode (got from profiler) related to TableView is still on memory and not released) So whenever i close a tableView i call the above code.

Also, I use inner classes to setCellFactory like below,

column.setCellFactory((TableColumn<?, ?> param) -> new EditingTableCell());

private class EditingTableCell extends TableCell<?, ?> {
//.....
}

So, How should i properly clean up a TableView instance so that it will be garbage collected.

Java Version : 1.8.0_45

JavFX Version : 8.0.45-b11

3 ( No strong reference to the table instance ) should be enough.

Listeners are weak references that do not prevent the GC from cleaning up the object, if there are no more strong references to it.

However, if the TableView is contained within a Tab, it might not be GC'd upon tab closing, because JavaFX in its internals keeps a strong reference to the last closed tab (a gotcha it took me a while to find out - I consider it an undesired bug-like behaviour).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM