0

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

A.Budziak
  • 27
  • 6
  • What I dont understand: you are sure that the JavaFx parts are to blame? Did you do any kind of memory profiling to figure where your memory is spent on? – GhostCat Dec 30 '16 at 11:44
  • What you see above is the whole program. There's nothing more than that, so I don't know if there can be anything else than JavaFx that's not working. – A.Budziak Dec 30 '16 at 11:47
  • 1
    Well, if that is really the whole program, then you want to add the missing parts to complete your class. "Almost complete" is still not [mcve] – GhostCat Dec 30 '16 at 11:48
  • Now I inserted everything what's in there. – A.Budziak Dec 30 '16 at 11:53
  • What is your memory setting ? -Xmx ? – Pascal Heraud Dec 30 '16 at 11:58
  • I would try out to insert `gc.clearRect(0, 0, 600, 600);` right before `drawImage()`. If it's a buffering issue with the canvas, this could fix it. – Calculator Dec 30 '16 at 12:02
  • -Xmx is 512m according to Intellij's idea.vmoptions file (not sure if it's the project heap size though). @Calculator clearRect didn't help. – A.Budziak Dec 30 '16 at 12:11
  • Does it actually run out of memory? – James_D Dec 30 '16 at 14:48
  • Have a look at [this question](https://stackoverflow.com/questions/40228866/optimizing-memory-leakage-in-javafx) too - it may be solved with new versions of Mesa and X.org – Itai May 26 '17 at 08:55

1 Answers1

3

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.

mipa
  • 10,369
  • 2
  • 16
  • 35
  • My code seems to be affected by this bug as well. I am simply drawing a star field and scrolling it. So about 10-20 ovals drawn per second. On Linux this increases memory to over 16GB in about a minute. On Windows this runs perfectly fine. Setting `-Dprism.order=sw` fixed this for me. Thanks. (I was getting paranoid ^^) – showp1984 Mar 03 '17 at 23:47