build
build copied to clipboard
[suggestion] detect physical memory and limit it by default
Why? To reduce some oom-killing events.
How? Cgroup can help we to limit system resources.
urain39@mgv2000:~$ systemd-cgls
Control group /: -.slice
|-user.slice | `-user-1000.slice
| |[email protected]
| | `-init.scope
| | |-1500 /lib/systemd/systemd --user | | `-1501 (sd-pam)
| |-session-2.scope
| | |- 1608 tmux new -s main
| | |- 1609 -bash
| | `-18440 qbittorrent-nox
| `-session-382.scope | |-18288 sshd: urain39 [priv]
| |-18371 sshd: urain39@pts/0
| |-18372 -bash
| |-19658 systemd-cgls
| `-19659 pager
|-init.scope
| `-1 /sbin/init
`-system.slice
|-haveged.service
| `-604 /usr/sbin/haveged --Foreground --verbose=1 -d 32 -w 1024
|-systemd-udevd.service
| `-474 /lib/systemd/systemd-udevd |-cron.service
| `-834 /usr/sbin/cron -f
|-system-serial\x2dgetty.slice
lines 1-28
The old property MemoryLimit
( and MemoryMax
) may kill process directly, but the new MemoryHigh
property not.
MemoryHigh=bytes¶ Specify the throttling limit on memory usage of the executed processes in this unit. Memory usage may go above the limit if unavoidable, but the processes are heavily slowed down and memory is taken away aggressively in such cases. This is the main mechanism to control memory usage of a unit.
MemoryMax=bytes¶ Specify the absolute limit on memory usage of the executed processes in this unit. If memory usage cannot be contained under the limit, out-of-memory killer is invoked inside the unit. It is recommended to use MemoryHigh= as the main control mechanism and use MemoryMax= as the last line of defense.
So we can simply set the properties of systemd to achieve it:
# Get physical memory size
MEMORY_SIZE_IN_KB=$(awk 'NR==1 { print $2 }' /proc/meminfo)
# By default, we only limit process of user
# MemoryHigh=0.5*MEMORY_SIZE
# MemoryMax=0.75*MEMORY_SIZE
systemctl set-property user.slice MemoryHigh=$((MEMORY_SIZE_IN_KB*50/100))K MemoryMax=$((MEMORY_SIZE_IN_KB*75/100))K