Systemd memory limit not working/example

memoryout of memorysystemd

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.

Best Answer

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
Related Question