Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java G2D Game CPU Hogging (And I mean 183%)  (Read 1693 times)
0 Members and 1 Guest are viewing this topic.
Offline Dankworth

Senior Newbie





« Posted 2013-07-20 13:37:42 »

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.
Offline Longor1996
« Reply #1 - Posted 2013-07-20 18:13:09 »

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.

1  
2  
3  
4  
5  
6  
7  
new Thread(new Runnable(){
  public void run(){
    try {
      Thread.sleep(Long.MAX_VALUE);
    }catch (Exception e) { }
  }
}).start();


- Longor1996

Sorry for my bad English! That's because i am from Germany.
Offline Dankworth

Senior Newbie





« Reply #2 - Posted 2013-07-20 18:44:56 »

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)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2013-07-20 20:49:31 »

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. Smiley

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.

Offline Dankworth

Senior Newbie





« Reply #4 - Posted 2013-07-20 23:38:18 »

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.
Offline Dankworth

Senior Newbie





« Reply #5 - Posted 2013-07-23 22:40:28 »

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?
Offline davedes
« Reply #6 - Posted 2013-07-24 00:00:50 »

Post some code...

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

Offline Dankworth

Senior Newbie





« Reply #7 - Posted 2013-07-24 00:08:17 »

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?
Offline Dankworth

Senior Newbie





« Reply #8 - Posted 2013-07-24 03:21:04 »

Figured it out! Wasn't using glPushMatrix() after each quad
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2013-07-24 06:48:58 »

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:

1  
2  
3  
4  
   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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dankworth

Senior Newbie





« Reply #10 - Posted 2013-07-24 16:25:19 »

I'm not entirely sure what you mean by that. For what purpose?
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2013-07-25 03:14:50 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Dankworth

Senior Newbie





« Reply #12 - Posted 2013-07-25 04:43:08 »

Ok.
Offline jonjava
« Reply #13 - Posted 2013-07-25 18:51:15 »

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (37 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (27 views)
2014-10-15 16:18:58

TehJavaDev (52 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (77 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!