Limiting java application's memory and cpu usage

匆匆过客 提交于 2019-12-29 08:32:20

问题


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.


回答1:


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




回答2:


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.




回答3:


In this scenario it might help to run the app on a Mobile emulator (E.g. Android).

With this you can emulate a mobile device with specific CPU/Memory. So, you should get performance this is comparable to a device that has slower CPU and lesser RAM.

Android / Nokia emulator are free and available for download from developer sections of Nokia/Google sites.




回答4:


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.




回答5:


Please, be careful with memory and CPU options when running jvm 8 or earlier. There are a couple of very nice articles about that. Check it out:

https://developers.redhat.com/blog/2017/03/14/java-inside-docker/

https://jaxenter.com/nobody-puts-java-container-139373.html

Having said that, containerization - is the right way to go for microservice architecture regardless of the stack, and jvm is not as exception to that. It is however, important to be aware of the caveats.




回答6:


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.




回答7:


For CPU you can try my new lib :).

https://github.com/dyorgio/cpu-watcher

Example of use:

// Limit process by PID to 25% of host cpu usage
CpuWatcher cpuWatcher = new CpuWatcher(pid, 25f);
cpuWatcher.start();


来源:https://stackoverflow.com/questions/4952528/limiting-java-applications-memory-and-cpu-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!