Optimizing memory leakage in JavaFX
<p>I have written a piece of code, to make letters appear and fly as I write them. The problem it consumes a lot of memory.</p>

<p>I already optimized it a little bit by</p>

<ul>
<li>Sharing the <code>path</code> object and update its parameters in listeners.</li>
<li>Calling gc each time a new letter is printed</li>
</ul>

<p>But it still uses a lot of memory, so any ideas about how to reduce its memory utilization ?</p>

<p>Thanks in advance.</p>

<pre><code>

    package sample;

    import javafx.animation.PathTransition;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.LineTo;
    import javafx.scene.shape.MoveTo;
    import javafx.scene.shape.Path;
    import javafx.scene.text.Font;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
    import javafx.util.Duration;

    public class Main extends Application {

        public static void main(String[] args) {
            launch(args);
        }

        @Override
        public void start(Stage primaryStage) throws Exception {
            Pane root = new Pane();
            Scene scene = new Scene(root);
            root.setCache(false);
            primaryStage.setTitle("Hello World");
            primaryStage.setScene(scene);


            Path path = new Path();
            root.widthProperty().addListener((observableValue, oldSceneWidth, newSceneWidth) -> SetPathElements(path, root));
            root.heightProperty().addListener((observableValue, oldSceneWidth, newSceneWidth) -> SetPathElements(path, root));


            Duration duration = Duration.millis(1000);

            scene.setOnKeyPressed(event -> {
                System.gc();

                Text textNode = new Text(event.getText());
                textNode.setFont(Font.font(50));
                textNode.setFill(Color.ORANGE);
                root.getChildren().add(textNode);


                PathTransition pathTransition = new PathTransition();
                pathTransition.setDuration(duration);
                pathTransition.setPath(path);
                pathTransition.setCycleCount(1);

                pathTransition.setNode(textNode);
                pathTransition.setOnFinished(event1 -> {
                    root.getChildren().remove(textNode);
                    pathTransition.setNode(null);
                    pathTransition.setPath(null);
                    textNode.setFont(null);
                    textNode.setFill(null);
                });
                pathTransition.play();


            });
            primaryStage.show();
        }

        private void SetPathElements(Path path, Pane root) {
            path.getElements().clear();
            double w = root.getWidth();
            double h = root.getHeight();
            path.getElements().add(new MoveTo(w / 2, h));
            path.getElements().add(new LineTo(w / 2, -40));
        }
    }

</code>
</pre>

<hr>

<h1>EDIT #1</h1>

<p>OS: Arch Linux 64-bit
Platform: Intel i7-3rd generation, 8 GB ram
IDE : Intellij
JDK : 1.8.0_102</p>

<p>Proof of leak : After typing around 100 chars it jumped from 50 MB to 1.3 GB
<a href="https://i.stack.imgur.com/PGqe6.png" rel="nofollow"><img src="https://i.stack.imgur.com/PGqe6.png" alt="Memory leakage proof"></a></p>

<hr>

<h1>EDIT #2</h1>

<p>I have checked Heap size using jvisualvm and it indicates that the Heap expands greatly but the used portion don't exceed ~50 MB
<a href="https://i.stack.imgur.com/BjJDo.png" rel="nofollow"><img src="https://i.stack.imgur.com/BjJDo.png" alt="enter image description here"></a></p>