I'm running Ubuntu 20.04 with systemd 245.

I'm testing out systemd's resource control features; particularly the memory usage controls. The relevant section of my unit file looks like this:



The systemd docs claim that when MemoryMax is reached, the service is killed by the "out of memory killer".

However, if I run code designed to come up against this limit, like this C++ code:

int main()
    while (true)
        char *p = new char[100];

Instead what happens (when I watch it in htop) is that the 'RES' memory climbs to 512MB and stops, but the 'VIRT' memory continues to climb and the allocations switch to using swap. The OS eventually kills the application when swap is exhausted.

If I use the LimitAS and/or LimitDATA options instead, the application gets killed when it hits 512MB, as expected.

What's going on here? systemd recommends using MaxMemory over LimitXXX (see here), so why don't they work in the same way?

  • What version of systemd are you running? /usr/lib/systemd/systemd --version or systemctl --version? Note the man page you've linked states for MemoryMax "This setting replaces MemoryLimit=.", so it could be within reason that you're on a version of systemd that isn't using MemoryMax. – Kahn Jul 2 at 14:28
  • version 245.4. I've also tried using MemoryLimit= and it gives similar results. MemoryMax is listed in my version's man pages so safe to assume it's present. – jezza Jul 2 at 16:05

The answer was to read the docs more carefully. In particular, this paragraph:

To ease the transition, there is best-effort translation between the two versions of settings. For each controller, if any of the settings for the unified hierarchy are present, all settings for the legacy hierarchy are ignored. If the resulting settings are for the other type of hierarchy, the configurations are translated before application.

I don't really understand the second sentence here, but evidently my system isn't using the unified hierarchy, and I had another, seemingly unrelated, setting (AllowedCPUs=) that is a unified hierarchy-only setting. Removing that option made MemoryMax= behave as expected.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.