简体   繁体   English

如何在Java(fx)中释放静态ObservableList

[英]How to release static ObservableList in Java(fx)

I am using a static observable list for a tableview in a javafx application. 我在javafx应用程序中为tableview使用静态可观察列表。

  public class TableData {
          private static ObservableList<MyObject> data = FXCollections.observableArrayList();

          public static ObservableList<MyObject> getData(){
                 return data;
          }
  }

When I load new Data I tried several approaches to remove the “old“ data to release memory, like 当我加载新数据时,我尝试了几种方法来删除“旧”数据以释放内存,例如

    TableData.getData().clear();

or 要么

    TableData.getData() = FXCollections.observableArrayList();

or even 甚至

     for(int i=0; i< TableData.getData().size(); i++){
           MyObject mo = TableData.getData().get(i);
           mo=null;
     }

But still no Memory is released (checked with netbeans Analyzer) 但是仍然没有释放内存(已通过netbeans Analyzer检查)

Can anyboy help? 男孩可以帮忙吗?

Your problem is not related to JavaFX. 您的问题与JavaFX无关。 In general you should learn how references are handled in java. 通常,您应该学习如何在Java中处理引用。 You can find an introduction here or here . 您可以在这里这里找到介绍。

Code like in your example ( TableData.getData() = FXCollections.observableArrayList(); ) do not change the internals of your static collection. 像您的示例中的代码( TableData.getData() = FXCollections.observableArrayList(); )不会更改静态集合的内部。 Next to this defining something like this as a static value is an anti pattern. 在此旁边将这样的东西定义为静态值是反模式。

Based on your question and sample I assume that you are quite new to the Java language. 根据您的问题和示例,我假设您是Java语言的新手。 I prefer to read a general Java book (see this link for a good overview). 我更喜欢阅读一般的Java书籍(请参阅此链接以获得很好的概述)。 This will help you to understand the general problems in your sample. 这将帮助您了解样本中的一般问题。

In general you have the same behaviour in mostly all object orientated languages so even a book / tutorial about OOD might be a good idea :) 通常,您几乎在所有面向对象的语言中都具有相同的行为,因此即使是有关OOD的书/教程也可能是一个好主意:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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