So I got a lot of my programming sorted out recently and managed to get a playable part of my game working. However, I saw horrid performance. Playing around with it for a while, I realized that it "goes away" after a while, doubling my FPS like a flip of a switch. The time it takes for it to start running decently is completely random. Sometimes it's instant, sometime's it's more than a minute, which is obviously unacceptable.First I obviously checked if anything else was running in the background. Nothing. Then I saw that my game's java.exe dances at around 40-45% processor usage during this time, dropping to 25% (100% on one of four cores) when the game starts running as it should. The game doesn't even create a single thread manually, so it should be impossible for it to use 100% on one core (25% in the task manager). I'm basically having a ghost thread draining CPU power in some weird way. I've tried to monitor threads with the profiler in Netbeans, but I can't see any thread stopping or pausing when the lag disappears.

Like I said, I'm not doing anything with threads. I'm gonna try to use that trick now.EDIT: Nope, nothing. Sigh... Just added a very CPU bound feature, and now it doesn't speed up even after waiting minutes. What the heck is doing that?! I've even tried both JDK 6 and 7!

Java runs multiple threads in and of itself, and if you aren't organising your program's components into threads, Java will try to run everything on a single thread. That being said, it will probably be really slow, and it will probably eat up CPU.

Find me on GitHub: connergdavisMy current project is called Obsidian Framework and is a private Minecraft server application used to play Multiplayer.

I'm not creating even a single Thread object, so the fact that another thread slows down my game seems to point to a problem with LWJGL. No problem occurs if I run the exact same code, but without rendering. The scary part is that I can't identify WHICH thread is eating performance.

FOUND IT! Damn you, Threaded optimization!The problem relates to the setting Threaded optimization in the Nvidia control panel! The ghost thread was a video driver thread! I've actually been experimenting with that setting. The default is Auto, which apparently tries to use multithreading when the game is started and then falls back to single threading when it realizes it gets better performance for less CPU power from that after often less than a second. HOWEVER, if what you're doing is very CPU heavy (i.e. the graphics card is idling a lot) which I was doing it will apparently not fall back as it should. Setting the setting to Off solves this.Thanks to everyone who posted, especially Riven! xD And yes, that is the exact same sleep fix that I used (except I didn't use an infinite loop, fixed xD).

It won't help, its a Windows specific fix in the JVM. As long as a thread is asleep the JVM keeps the resolution at 1ms to make waking up threads/locks/timers more precise. As soon as there is no sleeping thread the resolution is reset to the default 10 or 15ms (system dependent, happy happy joy joy).

Because the precision is already high enough (1-2ms) on Linux or Mac platforms out of the box, the JVM for those platforms does not have the workaround and so the sleeping thread trick adds nothing.

As apposed to the JVM itself, yes. Sun already attempted that once with a command line switch, but they managed to fail in a very epic way:

Wow, that is epic fail indeed. Whats even worse is that they want to keep the broken switch in there.

Wanted. Sun is gone, Oracle is now in charge. Seeing what the last dozen updates to Java 6 have brought, it is clear that Oracle is more inclined to apply fixes that break backwards compatibility. What I don't like is that they often do so silently :/

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