Edit(Solution): I was deriving a font each time the paintComponent method would be called which would then create a new font object each time using up several gigabytes of memory.
I'm currently developing a little game as a university project and its going pretty good but i stumbled upon some confusing behaviour that is going on when I call repaint()
. The game itself is really simple. There is just a player that can move around, jump and a little text displaying fps/ width and height of the window.
I have two threads. One is executing the game logic and one is painting/repainting.
I noticed that my application would consume up to several gigabytes of memory over time. After further investigation it seems that this behaviour is caused by calls go the drawString
method in the paintComponent
method which is called when I repaint the screen (a JPanel
).
The behaviour stays the same even if I limit the times the repaint method is called per second.
But if I comment out the calls to drawString
method my application suddenly only consumes around 20 to 50 megabytes of memory.
Here is what a call looks like:
g.drawString("FPS: " + this.getFPS() + " width: " + this.getWidth() + " height: " + this.getHeight(), 10, 15);
I inspected the memory behaviour with Java VisualVM. Maybe someone has an idea what is going on here.