4

as title is descriptive i want to restrict CPU usage of a java application that runs in windows to specific amount (namely 2 GH)

this app could be a ".jar" file or an app which runs by IntelliJ IDEA or Eclipse

Hamed Ehtesham
  • 147
  • 1
  • 7
  • 1
    Can you elaborate on what you need that for? I could understand limiting it to a certain number of cores or a percentage of available CPU but "2 GH" implies you want to limit speed - so _why_? – Thomas Feb 12 '16 at 14:22
  • 1
    i have the same issue , i wanna restrict speed of CPU for some measurement tasks which approach me to a good view of limitation when i have specific resources – Mahdi Javaheri Feb 12 '16 at 14:28
  • @Mehdi Is not speed that can be restricted but percentage of use. – Davide Lorenzo MARINO Feb 12 '16 at 14:30
  • This topic may help http://stackoverflow.com/questions/4952528/limiting-java-applications-memory-and-cpu-usage – Arnaud Feb 12 '16 at 14:32
  • @Thomas : i attended in a java programming challenge and they told us they give our program specific amount of CPU (namely 2 GH) and i wanted to test it on my laptop but its CPU speed is 3 GH and has 8 cores – Hamed Ehtesham Feb 12 '16 at 14:34
  • If it's like the challenges I know it's just an upper bound, i.e. they tell you what resources to expect at most. You'd want your software to run faster than what they provide so testing it to run "just" within the bounds might not be enough anyways. And most of those challenges allow you to do dry runs or submit multiple times anyway. – Thomas Feb 12 '16 at 14:49

2 Answers2

3

Limiting resources like CPU is an operating system related question.

No parameter in java can be used to limit per percentage of CPU used.

Additionally you don't limit the frequency of your CPU, but only the percentage of use. So if you have a 3Ghz CPU you can limit it at 66%. It means that no more than 66% of time the CPU will be used by java, but for this 66% it can be used at 100% of its speed.

Mansouri
  • 233
  • 4
  • 6
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • how can i do that? Plz help me if U can – Hamed Ehtesham Feb 12 '16 at 14:25
  • I don't know, but is better to ask again the question in a different form. Something like: "How it is possible to limit CPU usage to a process in windows?". Remember this is not related to java and probably in this form is not easy to find somebody that can help you. – Davide Lorenzo MARINO Feb 12 '16 at 14:27
  • and it's not really necessary to ask that question again, it has been asked before on SO and SU. – the8472 Feb 12 '16 at 16:39
0

The only way to limit a JVM without use an OS in a VM with limit CPUs, is to have the application limit itself. When it is using more CPU than you would like you have to make it sleep the threads it is running. There is no way to do this autogamically, instead you have to add this to you code, possibly in every place it could use a lot of CPU.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130