4

I have come across some recent lag spikes in a game I have been developing. It is consistent, happens around the same time. Using the java profiler jvisualvm I have found it occurs at the same time a particular thread seems to restart or something (AWT-EventQueue-0):

Image of Thread changing channels

Other than that, there is no visible cause, not in heap usage, processor use, memory space, or method uses. It will sometimes cause a ConcurrentModificationException when drawing my array of objects, but this should only happen with substantial lag, and my game is hardly intensive.

I don't recall performing any recent changes to the project, however I have carried out the following recently:

  • Updated java to the latest version
  • Downloaded latest version of JDK7 (though it is not being used in this project)
  • Fixed bug with eclipse that occurred as a result of installing JDK7 (removed 256m limit in eclipse.ini)

I am running Eclipse Indigo-service-1 on 32 bit XP. My processors are barely used.

Gray
  • 108,756
  • 21
  • 270
  • 333
Perry Monschau
  • 859
  • 8
  • 22
  • What is a "lag spike"? How are you measuring it? When the thread is "restarted", what is it doing? – Gray Mar 16 '13 at 20:58
  • 1
    The lag spike is where the game ends up around 3 FPS for a second or so, it's visible, practically renders the game unplayable since it's a reaction type game. I have no idea what the Thread is doing, I don't even know what the Thread is for. I could get the Thread dumps from jvisualvm if you like? – Perry Monschau Mar 16 '13 at 21:01
  • Let me note, the only thread handling I am doing is at initialisation where I start off the game loop. – Perry Monschau Mar 16 '13 at 21:02
  • 3
    @PerryMonshau AWT-EventQueue-0 is the [*Event Dispatch Thread*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html). Something you are doing on this thread is causing it to block, which causes your application to become unresponsive. We will need to examine your code to see what is happening. The issue with `ConcurrentModificationException`s is probably because your game loop is not running on the EDT, and you are not handling the concurrency issues. (Also, knowing which imports you are using without the code itself is next to useless.) – Jeffrey Mar 16 '13 at 21:03
  • And if you want to learn more about concurrency, take a look at [this tutorial](http://docs.oracle.com/javase/tutorial/essential/concurrency/) to get started. – Jeffrey Mar 16 '13 at 21:07
  • How would you recommend I submit my project's code? I have over 1000 lines in total. Also as an update, it seems the lag spike only occurs when I draw my game board to an image first rather than directly to the component. – Perry Monschau Mar 16 '13 at 22:27

1 Answers1

5

It seems you are doing too much on the Event Dispatch Thread (EDT). AWT-Event-Queue-0 looks to be the EDT. Additionally, your last comment says

...it seems the lag spike only occurs when I draw my game board to an image first rather than directly to the component.

You'll need to push some of your computations to other threads, and it sounds like drawing the game board is a good choice for this. Also, any AI you may have.

Your keyboard & mouse handlers run on the EDT, and the graphics updates need to too. But you can pre-render to an image (like you are currently doing) outside the EDT. And you can send the keyboard & mouse events to another thread via a BlockingQueue.

One other thing you could do is decouple your game-update rate from your frame-update rate.

But without any details, I can't give much more advice.

Update: (just read your bit about ConcurrentModificationException)

This can be caused by two different things:

  1. you are updating a collection (like your ArrayList) in a different thread from the one you are reading it in; or
  2. you are iterating through said collection and updating it in the loop.

Point 2 is easy to fix; but I'm afraid I can't teach thread safety in such a short amount of space.

Gray
  • 108,756
  • 21
  • 270
  • 333
Michael Deardeuff
  • 9,345
  • 4
  • 47
  • 67