Opposite behavior for "limits.memory.swap"?


I’m running LXD 5.0.2 using snap on a regular Ubuntu Server 22.04 with Ubuntus default 5.15.x kernel.

Recently I wanted to add swap space to avoid out-of-memory situations and stumbled across weird behaviors regarding “limits.memory.swap”.

I added the swap space on the host and set “limits.memory.swap=true” for my test containers.
I then tried to allocate more memory than my test container had available, but the process was killed! Swap did not work!

Then I set “limits.memory.swap=false” again, tried the same thing, but now the containers are correctly using swap space and the test processes are NOT being killed. Parts of the process is being moved to swap, even though I set swap to false.

What is up with this weird behavior?
Shouldn’t “limits.memory.swap” work exactly the opposite? Why are true and false both doing the exact opposite of what they should do?
Or maybe I am misunderstanding something?

If you need more info let me know.

Thanks a lot!

1 Like

limits.memory always control the total memory usage (RAM+swap).
limits.memory.swap is true by default so setting it to true did nothing in your case.

Setting limits.memory.swap to false turns the swappiness to the absolute minimum to try to force the container to run entirely in RAM with no swap usage.

Still difficult to understand the reverse logic.
From literal point of view, it doesn’t make sense, because setting a limit to false supposedly nulls the limit.

Is this how it was designed in first place?
limits.memory always control the total memory usage (RAM+swap).
limits.memory.swap=false excludes the swap from the above total limitation?

limits.memory.swap=true (default)
When memory hard limit exhausted, no swap will be used.

When memory hard limit exhausted, host swap will be utilized in container.

Yes, It’s very confusing.

limits.memory.swap=false, can use swap

limits.memory.swap=true, can not use swap.

1 Like

Perhaps was the original Idea, Double Negatives make Positive!
Limit(negative)…swap : false(negative)
which gives: Use swap (positive)

This is fixed in lxd 5.16 (currently in latest/candidate snap channel).

See https://github.com/canonical/lxd/pull/12044 :grin:

1 Like