我写了一段代码,在我写这些代码时让字母出现并飞起来.它消耗了大量内存的问题.
我已经对它进行了一些优化
path对象并在侦听器中更新其参数.但是它仍然使用了大量的内存,所以关于如何降低内存利用率的任何想法?
提前致谢.
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));
}
}
操作系统:Arch Linux 64位平台:Intel i7-3rd一代,8 GB RAM IDE:Intellij JDK:1.8.0_102
泄漏证明:在输入大约100个字符后,它从50 MB跳到1.3 GB

JavaFX中存在内存泄漏,Mesa> = 11.0(意味着任何最新的Linux发行版).JavaFX开发人员说它是Mesa中的一个错误,但我在Mesa中找不到错误报告(也不能提交一个,因为我不知道如何在JavaFX之外重现它).
目前唯一的解决方案是 -
1.使用较旧的Linux(关键是使用Mesa 10或更低版本)
2.使用NVidia GPU - 它们有自己的OpenGL实现,不依赖于Mesa.
3.使用Windows.
更新(2016年11月)
此问题似乎已在较新版本的Mesa和/或X.org中得到解决.更新到Mesa 13.0和X.org> = 1.18.4 应该可以解决这个问题.
相关链接:
| 归档时间: |
|
| 查看次数: |
2274 次 |
| 最近记录: |