2 Replies Latest reply on Mar 4, 2013 12:22 PM by csh

    Understanding and resolving Memory Leaks and Bindings - part 78


      we have repeatedly memory leaks in our application.

      As usually I start searching the cause in Bindings.

      Sometimes I solved them using WeakChangeListener instead of Bindings.

      Now I got another case, where I use custom bindings and I don't know how to approach them.

      Please take a look at the following sample code:
      import javafx.application.Application;
      import javafx.beans.binding.StringBinding;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.property.StringProperty;
      import javafx.scene.control.Button;
      import javafx.stage.Stage;
      public class TestApp2 extends Application {
          public static void main(String[] args) {
          private StringProperty test = new SimpleStringProperty();
          public void start(Stage stage) throws Exception {
              for (int i = 0; i < 1000000; i++) {
                  StringBinding stringBinding = new StringBinding() {
                      protected String computeValue() {
                          return "";
              System.out.println((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 + " KB");
      It illustrates a custom binding as I usually use it. (of course with more Observables of other types in the super.bind() method).

      However, it seems, that I can't release it again from memory, once i created it.

      dispose does nothing, and super.unbind() is protected.

      Is there any possibility to remove those memory leaks?

      If I don't super.bind(test) it, the memory is 805 KB, otherwise it is ~45000 KB.
        • 1. Re: Understanding and resolving Memory Leaks and Bindings - part 78
          Ok, I figured, I could just override dispose() und there call unbind().

          But, what if I don't have the possibility to call dispose() explicitly, like in a ListView's CellFactory's, when I don't know when something is diposed. (usually I used WeakChangeListener for that case, but here?)
          • 2. Re: Understanding and resolving Memory Leaks and Bindings - part 78
            From the doc:


            All bindings in our implementation use instances of WeakInvalidationListener, which means usually a binding does not need to be disposed. But if you plan to use your application in environments that do not support WeakReferences you have to dispose unused Bindings to avoid memory leaks.

            Doesn't that mean, that it should be garbage collected?

            But in reality it isn't.