Memory leak optimization in JavaFX

I wrote a code snippet so that the letters appear and fly when I write them. The problem is that it consumes a lot of memory.

I already optimized it a bit

  • Sharing an object pathand updating its parameters in listeners.
  • Call gc every time a new letter is printed.

But it still uses a lot of memory, so any ideas on how to reduce memory usage?

Thanks in advance.



    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));
        }
    }



EDIT # 1

OS: Arch Linux 64-bit Platform: Intel i7-3rd generation, 8 GB RAM IDE: Intellij JDK: 1.8.0_102

Leak proof: after entering about 100 characters, it jumped from 50 MB to 1.3 GB Memory leak leak


EDIT # 2

jvisualvm, , , ~ 50 enter image description here

+3
2

JavaFX Mesa >= 11.0 ( Linux). JavaFX , Mesa, Mesa ( , , JavaFX).
-
 1. Linux ( Mesa 10 )
 2. NVidia - OpenGL Mesa.
 3. Windows.

( 2016 .)
, , Mesa / X.org. Mesa 13.0 X.org >= 1.18.4 .

:

+5

Mesa 13.0.4 , .

-Dprism.order=j2d -Dprism.order=sw VM, JavaFX OpenGL, . , .

0

Source: https://habr.com/ru/post/1662475/


All Articles