On Thu, 1 Sep 2022 13:10:49 GMT, John Hendrikx <jhendr...@openjdk.org> wrote:

>> modules/javafx.base/src/main/java/javafx/beans/value/ObservableValue.java 
>> line 266:
>> 
>>> 264:      * {@code condition} evaluates to {@code true}. This allows this 
>>> {@code ObservableValue}
>>> 265:      * and the conditional {@code ObservableValue} to be garbage 
>>> collected if neither is
>>> 266:      * otherwise strongly referenced when {@code condition} becomes 
>>> {@code false}.
>> 
>> What happens if they are GC'd and the conditional becomes `true` later?
>
> Edit: I missed some nuance, below is the correct version.
> 
> There are three objects involved here: the parent observable, the new 
> observable and the conditional.
> 
> What `when` should achieve is to make the new observable and conditional easy 
> to garbage collect when the conditional is `false` (the parent observable is 
> considered to be long lived so GC doesn't really apply there).  The 
> conditional and new observable refer to each other so they must have a 
> similar life cycle.
> 
>> What happens if they are GC'd and the conditional becomes `true` later?
> 
> This can't happen, the conditional refers to the new observable, their life 
> cycles are tied to each other.
> 
> Strong references look like this when conditional is `true`:
> 
>       conditional <--> new observable <--> parent observable
> 
> When it is `false`:
> 
>      conditional <--> new observable --> parent observable
> 
> Conditional must have a similar life cycle as new observable if your purpose 
> is to use `when` to break strong references to allow for GC.

If I have a (dumb) method


void someMethod(ObservableValue<String> longLivedProperty) {
    ObservableValue<Boolean> condition = new SimpleBooleanProperty(true);
    ObservableValue<String> whenProperty = longLivedProperty.when(condition)
}


then shouldn't `condition` and `whenProperty` be eligible for GC even when 
`condition` holds `true`? If not, do I not get a memory leak because I can't 
change `condition` to `false` to allow garbage collection?

-------------

PR: https://git.openjdk.org/jfx/pull/830

Reply via email to