Docker memory limit is exceeded

Viewed 801

I have the following docker-compose file:

version: "2.4"
services:
  auto_check:
    image: python
    mem_limit: 97M

That upon startup runs the following python script:

x = 'a' * (2 * 10 ** 8)

As expected, the docker fails with error code 137.
However, when I set mem_limit: 98M, the error does not reproduce. Any idea why docker allows such an extreme memory limit violation?
The image size is 135MB (ubuntu20+python3).

(Running on WSL2-ubuntu:20.04)

Thanks for helping!

1 Answers

This happens because memswap_limit defaults to the value of mem_limit, meaning you get mem_limit regular memory and another memswap_limit ( == mem_limit) of swap memory, for a total of 2x mem_limit.

You can either set mem_limit to be half as much as your actual required limit, or alternatively you can set memswap_limit: 0M to disable swap altogether.

Running

/usr/bin/time --verbose python3 -c "x = 'a' * (2 * 10 ** 8)" 2>&1 1>/dev/null | grep 'Maximum resident'

on my machine we can see that

Maximum resident set size (kbytes): 203708

memory is used by this Python script at its peak. In MiB terms that's 203708000 / 1024 / 1024 ~ 194MiB. Half of that is ~97MiB. That's where the 98-99M limit you observed comes from. ~98MiB for regular memory, ~98MiB for swap, a total of ~194MiB, which is ~the peak memory consumption of python3 -c "x = 'a' * (2 * 10 ** 8)"

Related