Javafx growing memory usage when drawing image

2019-09-11 10:37发布

问题:

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.