This discussion is archived
2 Replies Latest reply: Mar 4, 2013 4:22 AM by csh RSS

Understanding and resolving Memory Leaks and Bindings - part 78

csh Journeyer
Currently Being Moderated
Hi,

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) {
        Application.launch();
    }

    private StringProperty test = new SimpleStringProperty();

    @Override
    public void start(Stage stage) throws Exception {

        for (int i = 0; i < 1000000; i++) {
            StringBinding stringBinding = new StringBinding() {
                {
                    super.bind(test);
                }

                @Override
                protected String computeValue() {
                    return "";
                }
            };
            stringBinding.dispose();
        }

        System.gc();
        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
    csh Journeyer
    Currently Being Moderated
    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
    csh Journeyer
    Currently Being Moderated
    From the doc:

    http://docs.oracle.com/javafx/2/api/javafx/beans/binding/Binding.html

    >
    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.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points