Docker Community Forums

Share and learn in the Docker community.

Memory-swap vs. memory-swappiness and using in docker-compose

I have some questions (questions in bold):
1.memory-swap and memory-swappiness


In “PREVENT A CONTAINER FROM USING SWAP” it says:

If --memory and --memory-swap are set to the same value, this prevents containers from using any swap.

But wouldn’t that be the same if I set memory-swappiness to 0?
It was not 100% clear for me from this part of documention.




2. docker-compose v3


In the above link it says:

cpu_shares, cpu_quota, cpuset, mem_limit, memswap_limit: These have been replaced by the resources key under deploy. deploy configuration only takes effect when using docker stack deploy, and is ignored by docker-compose.

Is there any way to use mem_limit, memswap_limit in docker-compose (no docker stack deploy) with file version 3? Or workarounds other ways to set swapping to 0?

Thanks!

Sorry again: Has really nobody an idea?

Ok found something here, that explains my second question:


Seems that compose file v3 is meant to be used in swarm mode and not with docker-compose and I have to stay in v2 if I want to have these features.

Now only question 1 is missing!

Why doesn’t docker simply document whatever happened to memswap_limit?
It cannot be specified in Compose 3.7 yaml and I can’t find any references to configuration related to “swap”.


This github issue was shut down:

This stackoverflow thread is just as confused as we are:

Hello everyone,
I have a strange behavior on my docker swarm nodes that I share here to understand if it is normal operation or if someone haveing any advice for me.
I start by saying that on all nodes I have run the following commands (obviously after I have restarted all swarm nodes):

sysctl vm.swappiness=0

sysctl vm.overcommit_memory=1

The apparent problem is that I’m noticing heavy swap usage without RAM being used.
I don’t know if it is a normal behavior or if someone can advise me on how to use swap only if the available RAM is low (or finished).
I don’t know if I was able to explain myself. This is not causing problems for nodes stability that are functioning normally without any issue, I was just alarmed to see if this may be a problem or not.
The RAM of each docker node is 32GB and the swap is 2GB.

Thanks for your attention.
Claudio

For more information about cgroups and memory in general, see the documentation for Memory Resource Controller.

–memory-swap details
–memory-swap is a modifier flag that only has meaning if --memory is also set. Using swap allows the container to write excess memory requirements to disk when the container has exhausted all the RAM that is available to it. There is a performance penalty for applications that swap memory to disk often.

Its setting can have complicated effects:

If --memory-swap is set to a positive integer, then both --memory and --memory-swap must be set. --memory-swap represents the total amount of memory and swap that can be used, and --memory controls the amount used by non-swap memory. So if --memory=“300m” and --memory-swap=“1g”, the container can use 300m of memory and 700m (1g - 300m) swap.

If --memory-swap is set to 0, the setting is ignored, and the value is treated as unset.

If --memory-swap is set to the same value as --memory, and --memory is set to a positive integer, the container does not have access to swap. See Prevent a container from using swap.

If --memory-swap is unset, and --memory is set, the container can use as much swap as the --memory setting, if the host container has swap memory configured. For instance, if --memory=“300m” and --memory-swap is not set, the container can use 600m in total of memory and swap.

If --memory-swap is explicitly set to -1, the container is allowed to use unlimited swap, up to the amount available on the host system.

Inside the container, tools like free report the host’s available swap, not what’s available inside the container. Don’t rely on the output of free or similar tools to determine whether swap is present.

PREVENT A CONTAINER FROM USING SWAP
If --memory and --memory-swap are set to the same value, this prevents containers from using any swap. This is because --memory-swap is the amount of combined memory and swap that can be used, while --memory is only the amount of physical memory that can be used.

–memory-swappiness details
A value of 0 turns off anonymous page swapping.
A value of 100 sets all anonymous pages as swappable.
By default, if you do not set --memory-swappiness, the value is inherited from the host machine.