Say, "run myApp.jar with cpu=800 and memory=1024"
Ive been doing java programming for many years and it is an embarrasment to ask this question. I don't even know whether this is possible or not. And if so, how?
What I just want know is if it is possible to set a java program's maximum memory and cpu usage. I suddenly thought of this because of I recently started developing mobile apps. I want to know how the app will behave on the device which has very limited memory and processor.
I saw physics engines with demo apps that run on the browser or I can execute on my PC. What if I run them on a mobile device? Will the performance be the same? Rather than developing a sample mobile app to test the library's performance, I'd rather run it with a specific cpu and memory using my PC first.
I tried googling by the way... all i found was monitoring and performance tuning. I may be using wrong keywords.
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.
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)
Java is also a very high-level Object-Oriented programming language (OOP) which means that while the application code itself is much easier to maintain, the objects that are instantiated will use that much more memory.
None. See above, but the CPU overhead used to manage the threads does not change based on the thread context.
you can limit memory usage by -Xmx option and you can limit CPU usage by setting priority of the process and/or CPU affinity.
The JVM has no control over CPU usage nor priority.
JVM has control over max/min Memory usage.
There is a workaround. It is possible to run each JVM in a separate [Docker container][1]. And control the resource (Memory, CPU, Network, IO) allocation for each container. That is exactly the added value of the Docker containers.
[1]: The JVM has no control over CPU usage nor priority. Yet you can run each JVM in a separate Docker container. And control the resource allocation foreach container. That is exactly the added value of the Docker containers.
Linux:
taskset -a -c 0,1,2,3 <program>
Run a program and its child threads only on cores 0, 1, 2, and 3.
Docker offers up resource management options for limiting the cpu access for running docker containers. Have a look at the CFS scheduler options available with docker run
from Limit a container's resources in the Docker documentation, such as:
--cpus=<value>
- Specify how much of the available CPU resources a container can use. For instance, if the host machine has two CPUs and you set--cpus="1.5"
, the container is guaranteed at most one and a half of the CPUs. This is the equivalent of setting--cpu-period="100000"
and--cpu-quota="150000"
. Available in Docker 1.13 and higher.--cpuset-cpus
- Limit the specific CPUs or cores a container can use. A comma-separated list or hyphen-separated range of CPUs a container can use, if you have more than one CPU. The first CPU is numbered 0. A valid value might be0-3
(to use the first, second, third, and fourth CPU) or1,3
(to use the second and fourth CPU).
These options are also available via docker-compose, when deploying a Docker swarm / stack, as mentioned in Compose file version 3 reference under resources
:
version: '3' services: redis: image: redis:alpine deploy: resources: limits: cpus: '0.50' memory: 50M reservations: cpus: '0.25' memory: 20M```
Note: that the legacy resource options in docker compose v2 are now limited to stacks in the migration to v3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With