If your project(or the program which runs it) is a systemd service, slice, mount, or socket you will need to set resource limits within its service/slice/mount/socket file rather than setting it using /etc/security/limits.conf
or ulimit
.
You can find your systemd unit files in /usr/lib/systemd/system/
or /etc/systemd/system
depending on your distribution.
To quickly make edits you can use systemctl edit <unit name>
to make 'drop-in' edits. Specifying --full
with the previous command will allow you to view the full service file while you edit(Essentially editting a copy and a replacing).
For your particular question it would be advisable to set:
MemoryHigh=<Bytes>
You can think of this directive as specifying the throttling point of memory usage for your program. You can add K,M,G, or T as a suffix to the to specify Kilobytes, Megabytes, Gigabytes, or Terabytes respectively.
You can also set:
MemoryMax=<Bytes>
Upon exceeding this directives value, an out-of-memory killer will invoked within the unit. The manpage for regarding these directives(systemd.resource-control) recommends using this as a 'last line of defense`. You may also specify K,M,G,T as with the previous directive.
There's also a deprecated setting that preceded the two above directives called MemoryLimit
which may be useful if you're not on a newer systemd release.
The two mentioned directives apply limits to the entire control group, which will prevent forking processes from being able to escape from the limits; However you can also set limits to the singular process using the LimitAS
(Limit address space) directive.
An example service file using MemoryHigh=
and MemoryMax=
[Unit]
Description=A foobar service that does foo and bar
After=network.target
[Service]
Type=simple
MemoryHigh=2500M
MemoryMax=3000M
ExecStart=/usr/bin/foobar-daemon start
ExecStop=/usr/bin/foobar-daemon stop
[Install]
WantedBy=multi-user.target
For full description of directives or for additional information;
see systemd.resource-control or man systemd.resource-control
and systemd.exec or man systemd.exec
ulimit
– DanieleGrassini Mar 01 '22 at 17:54man systemd.resource-control
rather than usingulimit
or/etc/security/limits.conf
to handle resource limits. – ReedGhost Mar 01 '22 at 21:15