brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: MemoryHigh= squeezes a hungry service's memory before anything is killed

Memory
~/tips/tip-memory-systemd-memoryhigh

sudo systemctl edit indexer.service

Slow a greedy service down and reclaim its memory at a soft limit.

A hard memory limit ends with a killed process. For a service that merely likes to hoard cache, I want something gentler.

Add a drop in for the service:

sudo systemctl edit indexer.service

Put this in:

[Service]
MemoryHigh=1G
MemoryMax=1500M

Then restart it:

sudo systemctl restart indexer.service
systemctl show -p MemoryHigh -p MemoryCurrent indexer.service

Above MemoryHigh the service is throttled and the kernel takes memory back from it aggressively. The man page calls this the main mechanism for controlling memory use. MemoryMax stays as the hard ceiling for when the soft one is not enough.

Percentages work too, relative to the installed RAM, so MemoryHigh=25% travels well between machines of different sizes.

I would watch the service for a while after setting it. A limit that is too low does not crash anything, it just makes the program crawl, which is harder to spot than an error. If response times get bad, raise the value.

This needs cgroup v2, which current distributions use. The limit is also not a guarantee: usage can go above it when the memory cannot be reclaimed.

Further reading: systemd.resource-control man page

More Memory tips lsmem --summary for f in /proc/[0-9]*/status; do sudo apt install smem sudo pmap -x 1234 leaks -atExit — ./myprogram --some-option All Memory tips (12) All topics

Link