15

Is there a way to limit both the number of cores that java uses?

And in that same vein, is it possible to limit how much of that core is being used?

rynojvr
  • 567
  • 2
  • 5
  • 17
  • Not from the java side, the operating system allocates threads to cores. Some very expensive software from VMWare is the only solution I'm personally familiar with :) – Affe May 07 '12 at 19:30
  • 1
    http://stackoverflow.com/questions/8882885/is-it-possible-to-force-an-existing-java-application-to-use-no-more-than-x-cores – rolve May 07 '12 at 19:33
  • 1
    possible duplicate of [Limiting java application's memory and cpu usage](http://stackoverflow.com/questions/4952528/limiting-java-applications-memory-and-cpu-usage) – Abizern May 08 '12 at 00:08

3 Answers3

5

You can use taskset on linux. You can also lower the priority of a process, but unless the CPU(S) are busy, a process will get as much CPU as it can use.

I have a library for dedicating thread to a core, called Java Thread Affinity, but it may have a different purpose to what you have in mind. Can you clarify why you want to do this?

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I'm creating a distributed processing mesh for a school project, and one of the requirements was to be able to limit resource usage. – rynojvr May 07 '12 at 23:13
  • The simplest thing to do is to start the process as very low priority. e.g. `nice -n 20` This means it will only use "free" cpu. This won't limit memory (you can use max heap size) or disk or network IO (you have to code that if you need it) but it will do most of what you want. – Peter Lawrey May 08 '12 at 09:26
3

I don't think that there are built-in JVM options to do these kind of tweaks, however you can limit CPU usage by setting priority and/or CPU affinity of the JVM process. If you are on Linux take a look at CPULimit that is an awesome tool to do these kind of resource limitations.

aleroot
  • 71,077
  • 30
  • 176
  • 213
  • Major downside of this ubiquitous, portable language; I can't believe this hasn't been addressed by the "java overlords" corporation, at least. – ILMostro_7 Sep 18 '15 at 20:28
2

https://github.com/haosdent/jcgroup jcgroup is your best choice. You could use this library to limit the CPU shares, Disk I/O speed, Network bandwidth and etc.

haosdent
  • 965
  • 2
  • 8
  • 17