systemd memory limit not working/example

14,136

You have the config parameters in the wrong section.

If you look in your logs, you should see:

Unknown lvalue 'MemoryAccounting' in section 'Unit'
Unknown lvalue 'MemoryHigh' in section 'Unit'
Unknown lvalue 'MemoryMax' in section 'Unit'

https://www.freedesktop.org/software/systemd/man/systemd.resource-control.html

The resource control configuration options are configured in the [Slice], [Scope], [Service], [Socket], [Mount], or [Swap] sections, depending on the unit type.

Thus you want:

[Unit]
Description="Start memory gobbler"
After=network.target

[Service]
ExecStart=/data/memgoble 8388600
MemoryAccounting=true
MemoryHigh=1024K
MemoryMax=4096K
Share:
14,136

Related videos on Youtube

ilya1725
Author by

ilya1725

C/C++ from 1996 LabVIEW Tcl

Updated on September 18, 2022

Comments

  • ilya1725
    ilya1725 almost 2 years

    I'm trying to use the systemd infrastructure to kill my memory leaking service when its memory usage reaches some value. The configuration file used is this:

    [Unit]
    Description="Start memory gobbler"
    After=network.target
    MemoryAccounting=true
    MemoryHigh=1024K
    MemoryMax=4096K
    
    [Service]
    ExecStart=/data/memgoble 8388600
    

    systemd version is 237. However, no matter what I set in the MemoryMax the kernel would kill the process on its own terms, usually when its memory consumption reaches almost the entire physical RAM. I'm running this on an embedded system with no swap.

    Anyone sees an obvious error in the configuration? Perhaps there are some other settings that I'm missing.

    • ctrl-alt-delor
      ctrl-alt-delor about 6 years
      Have you considered using ulimit older simpler, and should work in this case.
    • ilya1725
      ilya1725 about 6 years
      Unfortunately, I don't have a choice in this matter. systemd is the only option. Based on the man page the MemoryMax should work for a service. It just doesn't for some reason.
    • ctrl-alt-delor
      ctrl-alt-delor about 6 years
      you do have an option to consider it. It looks like you did.
    • phemmer
      phemmer about 6 years
      @ctrl-alt-delor ulimit doesn't work for memory restrictions. The last kernel to support it (RLIMIT_RSS) was 2.4.30, which is 12 years old. See unix.stackexchange.com/a/32375/4358
    • Paddy Newman
      Paddy Newman over 4 years
      I also had the error: Unknown lvalue 'MemoryMax' in section 'Service' I have an older version of systemd and so I had to use MemoryLimit instead of MemoryMax.
  • ilya1725
    ilya1725 about 6 years
    Good catch @Patrick. I did the change you suggested, but still get these errors: Jun 12 19:15:52 intel-corei7-64 systemd[1]: /lib/systemd/system/memgoble.service: Unknown lvalue 'MemoryHigh' in section 'Service' Jun 12 19:15:52 intel-corei7-64 systemd[1]: /lib/systemd/system/memgoble.service: Unknown lvalue 'MemoryMax' in section 'Service' Which is strange because based on the systemd manual this is where the configuration should reside.
  • phemmer
    phemmer about 6 years
    Check the man page on your system. Note the comments in the linked man page such as "MemoryMax= replaces MemoryLimit=" and others. The linked man page is the very latest version. Your system might not be running that version.
  • ilya1725
    ilya1725 about 6 years
    Yeap, the systemd version I thought I've had was actually much older, that was the problem.
  • Admin
    Admin about 2 years
    PSA: This version mismatch issue is a very common source of confusion. Note that a hugely popular distro like RHEL 7 and basically all derivatives (CentOS 7, Amazon Linux 2, etc.) are still stuck on version 219! My personal favorite trick for exploring supported directives is to grep through the systemd.directives man page like this: man systemd.directives | grep -A 2 -i memory. For instance, that cmd tells me I can learn about MemoryLimit with man systemd.resource-control.