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