问题描述
我写了一段代码,在我写这些代码时让字母出现并飞起来。它消耗了大量内存的问题。
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.
我已经优化了一点
- 共享
路径
对象并在侦听器中更新其参数。 - 每次打印新信件时调用gc
- Sharing the
path
object and update its parameters in listeners. - Calling gc each time a new letter is printed
但它仍然使用大量内存,所以有关如何降低内存利用率的想法吗?
But it still uses a lot of memory, so any ideas about how to reduce its memory utilization ?
提前致谢。
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代,8 GB ram
IDE:Intellij
JDK:1.8.0_102
EDIT #1
OS: Arch Linux 64-bitPlatform: Intel i7-3rd generation, 8 GB ramIDE : IntellijJDK : 1.8.0_102
泄漏证明:输入大约100个字符后,它从50 MB跳到1.3 GB
Proof of leak : After typing around 100 chars it jumped from 50 MB to 1.3 GB
我已经使用jvisualvm检查了堆大小,它表明堆大大扩展但使用的部分不超过~50 MB
I have checked Heap size using jvisualvm and it indicates that the Heap expands greatly but the used portion don't exceed ~50 MB
推荐答案
JavaFX中存在内存泄漏,Mesa> = 11.0(意味着任何最新的Linux发行版)。 JavaFX开发人员说它是Mesa中的一个错误,但我在Mesa中找不到错误报告(也不能提交一个,因为我不知道如何在JavaFX之外重现它。)
目前只有解决方案 -
1.使用较旧的Linux(关键是Mesa 10或更低版本)
2.使用NVidia GPU - 他们有自己的OpenGL实现并且不依赖于Mesa。
3.使用Windows。
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.
更新(2016年11月)
此问题似乎已在较新版本的Mesa和/或X中得到解决.ORG。更新到Mesa 13.0和X.org> = 1.18.4 应解决此问题。
相关链接:
- 。
- 。
- openjfx-dev discussion thread.
- reddit discussion.
- Mesa discussion.
- Related SO post: JavaXF 8 ProgressBar and ProgressIndicator with process=-1 memory leak on Linux
这篇关于优化JavaFX中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!