优化JavaFX中的内存泄漏

遮荫的阿特夫

我写了一段代码,使字母在我写的时候出现并飞行。该问题占用大量内存。

我已经对其进行了一点优化

  • 共享path对象并在侦听器中更新其参数。
  • 每次打印新字母时调用gc

但是它仍然使用大量内存,因此有关如何减少其内存利用率的任何想法?

提前致谢。



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



编辑#1

操作系统:Arch Linux 64位平台:Intel i7-3rd generation,8 GB ram IDE:Intellij JDK:1.8.0_102

泄漏证明:键入约100个字符后,它从50 MB跃升至1.3 GB 内存泄漏证明


编辑#2

我已经使用jvisualvm检查了堆大小,它表明堆大大扩展了,但使用的部分不超过〜50 MB 在此处输入图片说明

伊泰

There is a memory leak in JavaFX with Mesa >=11.0 (meaning any up to date Linux distribution). JavaFX developers say it's a bug in Mesa, but I couldn't find a bug report in Mesa (nor could I file one, as I don't know how to reproduce it outside of JavaFX).
The only solutions as of now are -
1. Use an older Linux (the key is having Mesa 10 or lower)
2. Use an NVidia GPU - they have their own OpenGL implementation and don't rely on Mesa.
3. Use Windows.

Update (November 2016)
This issue seems to have been resolved in newer versions of Mesa and/or X.org. Updating to Mesa 13.0 and X.org >=1.18.4 should solve this issue.

Related links:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章