Reputation: 569
I'm making a game with Java Swing and I've realized that after the game as been running for a while, 10 minutes or so, the graphics start lagging a lot. The game loop reports to be running at a constant FPS.
Right now I have it so that repaint is also called once the game loop fires. If I don't sync the graphics, it is so slow different parts being drawn is is visible on the screen. Task manager shows the memory it uses is pretty much the same, however CPU usage gradually increases over time. If I had to guess it would have to be that I'm not using Swing to draw correctly.
The process I currently use for drawing is something like this:
The panel is setDoubleBuffered(true)
.
Any idea as to what might be wrong?
EDIT:
I couldn't get my SSCCE to lag, so I went back and retested each draw function, and it turns out
int ilen = drawActorQueue.size();
for (int i = 0; i < ilen; i++)
{
GameActor oo = drawActorQueue.get(i);
oo.draw(g2d, viewX, viewY);
}
seems to be causing the problem.
I think its because drawActorQueue actually contains pointers to 2 types of objects and GameActor is just the base class.
Does this look to be the case? I think I can fix it by either using interfaces, or having the list instead point towards the specific arrays for the 2 children objects.
EDIT: !@#$%^ It was just me being incredibly stupid and not clearing a list. I'll never code or debug when I'm dead tired again.
Upvotes: 0
Views: 400
Reputation: 77454
Attach a profiler to your application.
For example, visualvm
. Any hotspot should turn up quickly with "sampling".
What is really nice about visualvm is that you can actually attach when the problem is really happening, but run at full speed before it happens.
It will also show you charts of the memory consumption and GC activity. If you see your application churn through a lot of memory in a short time, you may want to see if you can reduce that.
visualvm
is a super powerful, free tool for Java that should be in everybodys toolbox, and the first place to go to when you see performance issues.
Upvotes: 1
Reputation: 14378
Ensure that your app is not leaking memory and GC'ing heavily. Run the program with verbose GC logging on to see if it an issue.
Upvotes: 2