0

I am using a static observable list for a tableview in a javafx application.

  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)

Can anyboy help?

Hendrik Ebbers
  • 2,570
  • 19
  • 34
JustMe
  • 366
  • 1
  • 4
  • 14
  • That last example sets a local reference back to `null` so that won't do anything. Are we talking about heap space or system memory here? In general, Java is reluctant to return memory to the system. If we're talking heap space, the memory will be re-used automatically once it has been garbage collected. The Analyzer tool might have a button to run a GC cycle, so you can get a better idea if the memory was released. I donot think you need to do anything yourself, unless you are actually running into memory problems (ie. you get an Exception). – john16384 May 23 '19 at 07:38
  • If there are other references to the objects, clearing the list doesn't make any memory available for garbage collection: The `ArrayList` keeps the backing array's size and simply fills it with `null` and changes the list size stored in some field... – fabian May 23 '19 at 10:00

1 Answers1

1

Your problem is not related to JavaFX. In general you should learn how references are handled in 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. 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. I prefer to read a general Java book (see this link for a good overview). 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 :)

Hendrik Ebbers
  • 2,570
  • 19
  • 34