-m, --memory=""Memory limit (format: <number>[<unit>]). Number is a positive integer. Unit can be one of b, k, m, or g. Minimum is 4M.
--memory=""Memory limit (format: <number>[<unit>]). Number is a positive integer. Unit can be one of b, k, m, or g. Minimum is 4M.
--memory-swappiness=""Tune a container’s memory swappiness behavior. Accepts an integer between 0 and 100.(调整容器的内存swappiness行为。接受0到100之间的整数)
--memory-reservation=""Memory soft limit (format: <number>[<unit>]). Number is a positive integer. Unit can be one of b, k, m, or g.
--oom-kill-disable=falseWhether to disable OOM Killer for the container or not.
--kernel-memory=""Kernel memory limit (format: <number>[<unit>]). Number is a positive integer. Unit can be one of b, k, m, or g. Minimum is 4M.kernel memory 没有特殊需求,则无需额外设置
限制可用的 swap 大小
强调一下 --memory-swap 是必须要与 --memory 一起使用的。但是凡事没有绝对,如果你非要不添加--memory-swap 选项呢?
如:docker run -it --rm -m 100M ubuntu-stress:latest /bin/bash
按照官方文档的理解,如果指定 -m 内存限制时不添加 --memory-swap 选项或 --memory-swap 设置为 0 ,则表示容器中程序可以使用 100M 内存和 100M swap 内存。默认情况下,--memory-swap 会被设置成 memory 的 2倍。
We set memory limit(300M) only, this means the processes in the container can use 300M memory and 300M swap memory, by default, the total virtual memory size --memory-swapwill be set as double of memory, in this case, memory + swap would be 2*300M, so processes can use 300M swap memory as well.
如果按照以上方式运行容器提示如下信息:
WARNING: Your kernel does not support swap limit capabilities, memory limited without swap.
可参考 Adjust memory and swap accounting 获取解决方案:
To enable memory and swap on system using GNU GRUB (GNU GRand Unified Bootloader), do the following:
(1)Log into Ubuntu as a user with sudo privileges.
(2)Edit the /etc/default/grub file.
(3)Set the GRUB_CMDLINE_LINUX value as follows: GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
(4)Save and close the file.
(5)Update GRUB. $ sudo update-grub
(6)Reboot your system.
正常情况下, --memory-swap 的值包含容器可用内存和可用 swap。所以 --memory="300m" --memory-swap="1g" 的含义为:
容器可以使用 300M 的物理内存,并且可以使用 700M(1G -300M) 的 swap。--memory-swap 居然是容器可以使用的物理内存和可以使用的 swap 之和!
如果 --memory-swap 的值和 --memory 相同,则容器不能使用 swap。
下面的 demo 演示了在没有 swap 可用的情况下向系统申请大量内存的场景:
Swappiness is a Linux kernel parameter that controls the relative weight given to swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100 inclusive. A low value causes the kernel to avoid swapping, a higher value causes the kernel to try to use swap space.Swappiness
--memory-swappiness=0 表示禁用容器 swap 功能(这点不同于宿主机,宿主机 swappiness 设置为 0 也不保证 swap 不会被使用):
docker run -it --rm -m 100M --memory-swappiness=0 ubuntu-stress:latest /bin/bash
➜ ~ docker run -it --rm -m 100M --memory-swappiness=0 ubuntu-stress:latest /bin/bash
root@e3fd6cc73849:/# stress --vm 1 --vm-bytes 100M # 没有任何商量的余地,到达 100M 直接被 kill
stress: info: [18] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: FAIL: [18] (416)