Limiting java application's memory and cpu usage

后端 未结 8 644
南笙
南笙 2020-12-18 21:45

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

相关标签:
8条回答
  • 2020-12-18 22:20

    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();
    
    0 讨论(0)
  • 2020-12-18 22:24

    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.

    0 讨论(0)
  • 2020-12-18 22:26

    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.

    0 讨论(0)
  • 2020-12-18 22:29

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

    0 讨论(0)
  • 2020-12-18 22:32

    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.

    0 讨论(0)
  • 2020-12-18 22:32

    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.

    0 讨论(0)
提交回复
热议问题