Javafx growing memory usage when drawing image

邮差的信 提交于 2020-01-06 19:37:26

问题


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.

Here's the code:

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

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

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


回答1:


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.



来源:https://stackoverflow.com/questions/41395696/javafx-growing-memory-usage-when-drawing-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!