Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java limiting resource usage

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?

like image 768
rynojvr Avatar asked May 07 '12 19:05

rynojvr


People also ask

How do I limit RAM usage in Java?

The short answer is that you use these java command-line parameters to help control the RAM use of application: Use -Xmx to specify the maximum heap size. Use -Xms to specify the initial Java heap size. Use -Xss to set the Java thread stack size.

How can I reduce CPU usage in Java?

You can set the priority of your whole Java application process to be lower than other applications; that's what the "nice" or "renice" commands do for your on Unix (or Mac OS X). Alternatively, and I think nicer, you can start a reduced-priority thread within your application, to do the CPU-intensive work.

How do I set JVM memory limit?

use the arguments -Xms<memory> -Xmx<memory> . Use M or G after the numbers for indicating Megs and Gigs of bytes respectively. -Xms indicates the minimum and -Xmx the maximum. you may want to look at MaxPermSize as well.


2 Answers

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?

like image 62
Peter Lawrey Avatar answered Sep 22 '22 06:09

Peter Lawrey


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.

like image 42
aleroot Avatar answered Sep 19 '22 06:09

aleroot