Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throttling CPU from within Java

I have seen many questions in this (and others) forum with the same title, but none of them seemed to address exactly my problem. This is it: I have got a JVM that eats all the CPU on the machine that hosts it. I would like to throttle it, however I cannot rely on any throttling tool/technique external to Java as I cannot make assumptions as to where this Vm will be run. Thus, for instance, I cannot use processor affinity because if the VM runs on a Mac the OS won't make process affinity available.

What I would need is an indication as to whether means exist within Java to ensure the thread does not take the full CPU.

I would like to point straightaway that I cannot use techniques based on alternating process executions and pauses, as suggested in some forums, because the thread needs to generate values continuously.

Ideally I'd like some mean for, for instance, setting some VM or thread priority, or cap in some way the percentage of CPU consumed.

Any help would be much appreciated.

like image 873
Diferdin Avatar asked Sep 06 '12 14:09

Diferdin


People also ask

How can I reduce CPU usage in Java?

Similarly, if a developer chooses to use the older Hashtable over a HashMap, synchronization may needlessly consume clock cycles. Choose the wrong Java collection class, and application performance will suffer. Choose the correct collection classes, and your high Java CPU usage problems will disappear.

How do I throttle my CPU?

Go to Control Panel > Hardware and Sound > Power Options: The Power Saver and Balanced modes will both throttle your CPU under different circumstances, in order to save power, lower temperatures, and reduce overall system noise.

Why CPU usage is high in Java application?

Java applications may take high CPU resources for many reasons: Poorly designed application code with inefficient or infinite loops. Inefficient algorithms (poor application logic) Recursive method calls (causing hundreds of recursions)

Is CPU throttling good?

In theory, CPU throttling is not bad and is a safeguard that has been built into your PC or laptop to keep it from accidentally damaging itself.


2 Answers

What I would need is an indication as to whether means exist within Java to ensure the thread does not take the full CPU.

There is no way that I know of to do this within Java except for tuning your application to use less CPU.

  • You could put some Thread.sleep(...); calls in your calculation methods. A profiler would help with showing you the hot loops/methods/etc..
  • Forking fewer threads would also affect the CPU used. Moving to fixed sized thread-pools or lowering the number of threads in your pools.
  • It may not be CPU that is the problem but other resources. Watch your IO bandwidth for example. Slowing down your network or disk reads/writes might restore your server to proper operation.

From outside of the JVM you could use the ~unix nice command to affect the priority of the running JVM to not dominate the system. This will give it CPU if available but will let other applications get more of the CPU.

like image 104
Gray Avatar answered Oct 12 '22 12:10

Gray


I take it you want something more reliable than setting the threads' priorities?

If you want throttled execution of some code that is constantly generating values, you need to look into chunking up the work the thread(s) do, and coding in your own timer. For example, the java.util.Timer allows for scheduling execution at a fixed rate.

Any other technique will still consume as much CPU as is available (1 core per thread, assuming no locks preventing concurrent execution) when the scheduler doesn't have other tasks to prioritize ahead of yours.

like image 42
Dilum Ranatunga Avatar answered Oct 12 '22 12:10

Dilum Ranatunga