Adjusting Linux OOM killer priorities with systemd
Use systemd's OOMScoreAdjust= setting to influence which services the Linux kernel kills when the system runs out of memory.
- Lower values make a process less likely to be killed.
- Higher values make a process more likely to be killed.
- The valid range is
-1000to1000.
Suggested priorities
| Service | OOMScoreAdjust |
|---|---|
| MariaDB | -500 |
| Varnish | -300 |
| Solr | 300 |
| ClamAV | 500 |
These are relative preferences rather than a guarantee: the kernel also considers factors such as each process's memory usage.
Configure the services
Create a systemd drop-in for MariaDB:
sudo systemctl edit mariadb.service
[Service]
OOMScoreAdjust=-500
Repeat for the remaining services with their corresponding values:
sudo systemctl edit varnish.service
sudo systemctl edit solr.service
sudo systemctl edit clamav-daemon.service
Apply the changes:
sudo systemctl daemon-reload
sudo systemctl restart mariadb varnish solr clamav-daemon
Verify the settings
systemctl show mariadb varnish solr clamav-daemon \
--property=OOMScoreAdjust
To inspect the kernel setting for a service's main process directly:
pid=$(systemctl show --property=MainPID --value mariadb.service)
cat "/proc/${pid}/oom_score_adj"
Find distribution-specific unit names
Service names vary between distributions. For example, ClamAV might use clamd@scan.service instead of clamav-daemon.service.
systemctl list-unit-files | grep -E 'maria|varnish|solr|clam'
Caveats
Avoid OOMScoreAdjust=-1000 unless a service must effectively never be killed. Making essential memory consumers immune can leave the kernel without a safe way to recover.
OOMScoreAdjust= controls the kernel OOM killer. If the journal says systemd-oomd selected the service, configure systemd-oomd's cgroup policy separately—for example, investigate ManagedOOMPreference=avoid.
Links
- [[Notes/Tech/Server memory usage]]
- [[Notes/Tech/Varnish varnishadm quick reference]]