Javafx在绘制图像时增加内存使用量 [英] Javafx growing memory usage when drawing image

查看:799
本文介绍了Javafx在绘制图像时增加内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaFX中创建了一个非常简单的Go棋盘游戏。我偶然发现我的应用程序中的内存使用量不断增加,并且在减少了所有不必要的内容之后,即使是最小示例也会导致超时内存增长,大约 50到100MB / s

I'm creating a quite simple Go board game in JavaFX. I stumbled on growing memory usage in my application and after reducing everything unnecessary, it appeared that even the minimal example causes huge memory growth overtime, its about 50 to 100MB/s.

以下是代码:

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
    Group root = new Group();
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 600, 600));
    Canvas canvas = new Canvas(600, 600);
    Image bg = new Image("resources/images/background2.jpg", 600, 600, false, false);
    root.getChildren().add(canvas);
    GraphicsContext gc = canvas.getGraphicsContext2D();
    new AnimationTimer() {
        @Override
        public void handle(long l) {
            gc.drawImage(bg, 0, 0, 600, 600, 0, 0, 600, 600);
        }
    }.start();
    primaryStage.show();
}

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

删除gc时不会出现问题.drawImage行,但显然不是解决方案。

The problem doesn't occur when I delete the gc.drawImage line but that's, obviously, not a solution.

Btw。我正在使用Arch Linux 64位与OpenJDK 8

Btw. I'm using Arch Linux 64-bit with OpenJDK 8

推荐答案

Linux上的JavaFX中有许多关于内存泄漏的错误报告。
例如 JDK-8156051 JDK-8161997
要验证您是否遇到此错误,请尝试使用 -Dprism.order = sw 运行程序,并查看错误是否仍然存在。

There are numerous bug reports about memory leaks in JavaFX on Linux. For example JDK-8156051 or JDK-8161997. To verify if you are hit by this bug try to run your program with -Dprism.order=sw and see if the bug persists.

这篇关于Javafx在绘制图像时增加内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆