import java.lang.ref.WeakReference;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class SceneLeak extends Application {

    static WeakReference<Scene> ref;

    public static void main(String[] args) {
        Application.launch(args);
    }

    public void start(Stage stage) {
        Platform.setImplicitExit(false);

        final Scene scene = new Scene(new NodeWithDependency(new Dependency()));
        ref = new WeakReference<>(scene);
        stage.setScene(scene);

        stage.setWidth(200);
        stage.setHeight(100);
        stage.show();

        startKeepAliveThread();
    }

    private static void startKeepAliveThread() {
        new Thread(() -> {
            while (true) {
                try {
                    //Force gc for good measure
                    System.gc();
                    System.runFinalization();
                    if (ref != null) {
                        System.err.println("scene = " + ref.get());
                    }
                    Thread.sleep(3_000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    public static class NodeWithDependency extends StackPane {

        private final Dependency dataDependecy;

        public NodeWithDependency(Dependency dependency) {
            this.dataDependecy = dependency;
            getChildren().add(new ScrollPane(new TextFlow(new Text("The quick brown fox jumps over the lazy dog"))));
        }
    }

    public static class Dependency {

    }
}
