Java G2D Game CPU Hogging (And I mean 183%)

Hey guys, I’m making a multiplayer game with a packet system and interpolation for its loops, everything works great and no speed issues, but it literally uses 183% of my CPU, while other Java based games (minecraft, etc) use virtually nothing comparatively. I’ve tried several different implementations of Thread.sleep() but I can’t do a sleep for longer than a few ms, so my cpu reduction is minimal. Any ideas? (Code below is the game loop class, there are also two other threads running the packet system (two while loops, one stuck on input.read until a packet comes in and the other checking an array of packets)

http://pastebin.java-gaming.org/8e5d9385d6d

EDIT: I can’t do a long sleep because it will disrupt the game loop.

Try to use a timer-hack thread, to force Java to use the high-precision timer thingy.


new Thread(new Runnable(){
  public void run(){
    try {
      Thread.sleep(Long.MAX_VALUE);
    }catch (Exception e) { }
  }
}).start();

  • Longor1996

Went ahead and tried that, no luck. Still getting 180-200% CPU usage. Maybe I put it in the wrong place? (Tried start class and before the loop starts)

Most Java-based games (or games written in any language these days) use OpenGL for rendering. This allows you to offload rendering to the GPU, and greatly reduces the CPU load. Really this is the first step you should take if you plan to make a game more complex than Pong. :slight_smile:

You are also using a lot of Thread.sleep in your game loop, which seems unusual. You can try some other game loops (search the forums) to see how they perform.

Wow, I had no idea OpenGL could even be used for 2D rendering, I noticed your tutorial link, I’ll have to give them a read. Thanks!
EDIT: The Thread.sleep()s were a last ditch effort someone recommended as a fix.

While working on overhauling my drawing system to OpenGL, I’ve run into a problem. I got it to start drawing my GUI, but it will only draw two quads. It goes through the code of drawing the rest but it doesn’t show them! Any ideas why?

Post some code…

Also if you are making GUIs you might find this particularly useful:
http://libgdx.badlogicgames.com/

http://pastebin.java-gaming.org/d98dd665f69 - My Game Loop
http://pastebin.java-gaming.org/5d98d5d566f - Render() GUI.paint() & Component.paint()

Sorry about that, don’t know why I didn’t post code in the first place. As for libraries, I avoid them because I’m afraid they could be inefficient or protected so I can’t produce a product with them. Is that the case, or does it not matter?

Figured it out! Wasn’t using glPushMatrix() after each quad

When using LWJGL, use Display.sync(60).

When not using LWJGL, use my Sync.sync(…) code in the LWJGL repo (Display.sync(…) simply calls Sync.sync(…)), and make the following change, to remove the dependency on LWJGL:


	private static long getTime() {
-		return (Sys.getTime() * NANOS_IN_SECOND) / Sys.getTimerResolution();
+		return System.nanoTime();
	}

It basically squeezes in as much Thread.sleep(1) calls as possible, and then fills the remainder with as few as possible Thread.yield() calls.

I’m not entirely sure what you mean by that. For what purpose?

Throttling the game-loop to a fixed frequency, at minimum CPU usage. :persecutioncomplex:

Ok.

If you look at the full source of the Sync class that riven linked in his post ( https://github.com/LWJGL/lwjgl/blob/master/src/java/org/lwjgl/opengl/Sync.java ) you can see exactly how it works. It’s very well commented, you should take a look!

Basically it uses a bit of maths in order to use as little CPU as necessary without sacrificing frames.