25.9. Using systemd unit files to set limits for applications
The systemd service manager supervises each existing or running unit and creates control groups for them. The units have configuration files in the /usr/lib/systemd/system/ directory.
You can manually modify the unit files to:
- set limits.
- prioritize.
- control access to hardware resources for groups of processes.
Prerequisites
- You have root permissions on the system.
Procedure
Edit the
/usr/lib/systemd/system/example.servicefile to limit the memory usage of a service:… [Service] MemoryMax=1500K …The configuration limits the maximum memory that the processes in a control group cannot exceed. The
example.serviceservice is part of such a control group which has imposed limitations. You can use suffixes K, M, G, or T to identify Kilobyte, Megabyte, Gigabyte, or Terabyte as a unit of measurement.Reload all unit configuration files:
# systemctl daemon-reloadRestart the service:
# systemctl restart example.service
Verification
Check that the changes took effect:
# cat /sys/fs/cgroup/system.slice/example.service/memory.max 1536000This output shows that the memory consumption was limited at around 1,500 KB.