Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting java application's memory and cpu usage

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.

like image 701
demotics2002 Avatar asked Feb 10 '11 01:02

demotics2002


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.

Why is Java taking up so much CPU?

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)

Why is Java taking up so much memory?

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.

Does waiting thread consume CPU?

None. See above, but the CPU overhead used to manage the threads does not change based on the thread context.


4 Answers

you can limit memory usage by -Xmx option and you can limit CPU usage by setting priority of the process and/or CPU affinity.

like image 120
bestsss Avatar answered Oct 06 '22 00:10

bestsss


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.

like image 31
Dudi Boy Avatar answered Oct 05 '22 23:10

Dudi Boy


Linux:

taskset -a -c 0,1,2,3 <program>

Run a program and its child threads only on cores 0, 1, 2, and 3.

like image 37
Mr. B Avatar answered Oct 06 '22 01:10

Mr. B


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 be 0-3 (to use the first, second, third, and fourth CPU) or 1,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.

like image 29
zslb Avatar answered Oct 06 '22 00:10

zslb