If you run several services on one small machine, then one badly behaved program can eat the memory that all of the others need. A service with no memory limit does not fail politely. It takes the whole machine down with it, and it does that while somebody is using it.
I learned this the ordinary way, which is to say I learned it from the kernel. A container on a 4 GB guest grew to 2.9 GB, then peaked at 3.8 GB, and then the kernel killed the process in the middle of a conversation. The application had no idea. There was no exception, no stack trace and no shutdown message. From inside the program, nothing had gone wrong, because nobody had asked the program for its opinion.
Where the evidence lives
3 places hold the evidence, and only the last one tells you what actually happened.
- application log: nothing after the last successful request
- systemd journal: the unit restarting, with no reason it understood
- kernel log:
Out of memory: Killed process <pid> (<name>)
The first line looks like good news, and it is not. A program that stops without writing a single word to its own log did not decide to stop. Something outside it made that decision, and that something wrote the decision down.
That third line names the process, the moment and the state of memory at the time. Once you have it, the fix is usually 2 lines in a unit file. Without it, the same failure looks like a bug in your own code, so you go and read code that was never the problem.
The 2 settings
systemd gives you 2 memory thresholds, and they do different jobs.
[Service]
MemoryHigh=2600M
MemoryMax=3300M
MemoryHigh is the soft limit. When a service crosses it, the kernel starts to reclaim memory. Pages get written out to disk, the service slows down, and the machine stays up. It is a throttle rather than a wall.
MemoryMax is the hard limit. When a service crosses that one, the kernel kills the unit. Notice the word unit, because it matters whether the kill lands on the unit or on the machine. A killed unit restarts by itself in 5 seconds, and the damage stays inside 1 service. A killed guest takes every other container on the box down with it, and it stays down until a human notices.
That difference is worth more than it sounds when you are the human.
Never set MemoryMax without MemoryHigh. You want the throttle to bite first, so that a service slowing down is your early warning and the kill is only the backstop. Set the maximum alone and the first signal you get is a small funeral.
Why it matters more on small hardware
On a 64 GB host, a runaway service eats memory you were not using anyway. There is room for it to be stupid, and the bill arrives as a slightly slower evening.
On a 4 GB guest, the same service eats the memory that everything else was standing in. The services that fail are the innocent ones, so the reverse proxy goes down, then the database, then the thing you actually cared about. The guilty process dies first and dies cleanly. Everything behind it dies as collateral, and that is the part that costs you the evening.
That is the asymmetry people miss when they size a guest by what it needs at idle. The number that matters is not the steady state, because the steady state is the easy part. The number that matters is the worst case, and the worst case is usually set by a library’s default rather than by your design. That means a library you have never opened gets a vote on your uptime.
Prove the number
Limits are easy to set and easy to set wrong, and a limit that does nothing looks exactly like a limit that works. 2 commands tell you which one you have.
systemctl show <unit> -p MemoryCurrent -p MemoryHigh -p MemoryMax
systemctl show <unit> -p NRestarts
The first prints current use next to both ceilings. That way you can see how much room is left, instead of guessing at it. The second one prints the restart count for the unit, and that is the number I trust most. A restart count that climbs on its own is not a sign of a busy service. It is a sign that something is removing the service and not telling you.
After the fix, 3 consecutive sessions of the workload settled at 2.1 GB, with MemoryHigh at 2.6 GB, MemoryMax at 3.3 GB, and a restart count of zero. 3 sessions is not proof of anything and I will not pretend that it is. It is a claim about this particular workload, which is why the ceiling stays where it is instead of being removed once things looked calm.
Why this is a sovereignty argument
A hosted service that runs out of memory is somebody else’s incident. A self-hosted one is yours, at whatever hour it happens, and the collateral is every other service you run on that box.
Owning your infrastructure means owning the failure envelope as well as the hardware. You decide what a service is allowed to consume, and you decide what should happen when it tries to consume more. The alternative is finding out the way I did, from the kernel, on a Tuesday.
The catch
A ceiling contains a leak, and containing is not the same as fixing. Suppose MemoryHigh is visibly working. You can see sustained reclaim pressure, and a service that gets slow under load it used to handle without complaint. Then you have a memory bug with a limiter on it. The limiter is buying you time rather than health. Time is worth having, but it is not a cure.
The second catch is that limits interact with each other. A container inside a limited guest has 2 ceilings, and the tighter one wins. If you set 2 GB inside a guest with a 1.5 GB cgroup cap, then the inner number is decoration. Read both before you trust either.
What the numbers looked like from outside
One detail is worth recording, because it is the thing that made the diagnosis possible. The unit’s restart counter told the story before the memory graph did. A service that restarts on its own, with a clean exit code and no error in its own log, is not crashing. Something is removing it, and the kernel is the only party that admits it.
Once you know to look there, the sequence is obvious in hindsight. Memory climbs, the counter goes up, and the kernel names the victim. Before you know to look, the same evidence reads like an unstable application. So you go into the code hunting for a fault that is not there. That is a reliable way to lose a weekend and rewrite a function that was fine all along.
Where to start
Start with the services you would not want to lose, and set a ceiling on the ones next to them first. Not because the loud service matters less, but because the quiet ones are what a runaway neighbour takes out.
Then leave the numbers in the unit file, where they survive a reboot and a rebuild. A shell command solves the problem for one afternoon, and an afternoon is not a policy. The whole point of the lesson is that the failure returns when the configuration does not.
It is a small piece of the same argument as running your own AI on your own machine. The box is yours, so the failure envelope is yours too, and it is cheaper to set than to discover. The setting costs 2 lines. The discovery costs an evening and a small amount of dignity.
