CPU usage

Hi all,

The CPU usage is currently at about 12-32 CPU usage for the java instance for a game I’m making, which seems quite high. I’ve made quite a bit of the game now and it’s quite difficult for me to go through and isolate the source of the leak, although when I disable the tick of my game, the usage doesn’t drop that significantly, but when I disable the rendering, it drops to about 8-20 so I was wondering whether there is any program or addon for eclipse that could assist me in finding where I’m going wrong? I would post my code, but it has about 20 classes now. :stuck_out_tongue:

Anyway, in brief, I’m looking for a sort of static analysis program or any tips for reducing CPU usage. :] Please ask to see any section of my program.

Thanks,

Paul

You’ll want a profiler like VisualVM ( http://www.oracle.com/splash/java.net/maintenance/index.html ), then you can capture your game running and see where your CPU time is going.

Although 10/30% cpu isn’t particularly high - how are you monitoring that? Bear in mind that most user-facing CPU monitors (like Task Manager) are not particularly accurate and may include time your app is actually idle but waiting on drivers/sleep/other.

Yeah, I’ve just been using task manager. But I have been drifting in and out of a few source code samples, as I’m still quite new to 2d game development. I’ll give that VisualVM a go, thanks!

VisualVM is good, and if you’re using NetBeans, it has a built-in profiler that’s also quite nice. Eclipse has TPTP, which I gave up on, and there’s also JVMMonitor which looks quite nice, though I haven’t tried it.

built-in profiler of netbeans == VisualVM

I’ll try that JVMMonitor too then. Thanks mate!

Hi again, I’ve just been using that JVM monitor, which has led me to quite a few problems with my rendering hierarchy. That’s taken it from a stable 25-30 CPU usage to about 5-15, which is great. But I keep getting these spikes every few seconds to about 20-25 and at one point it just went back to 30. Is there anything in the CPU tab in JVM that I should be looking out for?

Thanks,

Paul

Check your memory graphs: are those spikes coincident with garbage collections?

Another question: How many cores does your computer have? On a hyperthreaded quad core (8 logical cores visible in Task Manager) the maximum utilization you can get is 12.5% without using threads. 12.5% therefore means that you have 100% usage on one core…

I’ve got a duel core processor. What’s odd is that sometimes it will appear to have far less CPU usage than other times. Sometimes it stays on average less than 15 and then in other instances it can hover around 25-30.

I’ve just taken out the method call that clears the screen to black before re-rendering as there will be no instance where the previous tiles will show in the current tiles and that’s dropped the CPU usage significantly. It’s still not quite as low as I’d hoped and it still has a few unexplainable spikes, though. I sort of get the general jist of what these features mean in JVM monitor, but I’m not really sure of how the “Time %” header works in the CPU section. For example, the main render method in my top class appears to use roughly 65%, but if you expand by clicking all the arrows of the methods that are called within that render method, they are much lower than 60% so it doesn’t list everything. Does anyone know how to make it list everything?

If you take that 65% and subtract all percentages of the methods it calls, you end up with the percentage that the method itself takes.

If a method takes 65% of your time, all the methods under it add up to 65%. They’re all absolute percentages, not relative to the parent. Any profiler should have a section when you expand that tells how much of that percentage was spent in the method body itself as opposed to other methods it called. It is easier to profile when you break things up into smaller methods though.

Here’s a screenshot of it while it’s running. The arrow points at the main render method, which calls all the other render methods, but everything called by that method doesn’t seem to add up to 50%

It adds up to 43.5% out of 49.6%.

Close enough.

But it adds up to 3.7 D:

self time

Ah, I see. Does anything look worryingly high on there?

Impossible to say without seeing the code. I’d start looking into what in Main.render is taking so much time.

Well the main render method calls the level render method, which calls the render methods of all the tiles and mobs within a certain radius. I’d imagine it would take the most time to process. Really what I want to isolate is specifically why the cpu usage fluctuates so erratically.