DEV Community

Cover image for What Happens When Linux OS Runs Out of Memory?
Anurag Vishwakarma
Anurag Vishwakarma

Posted on

What Happens When Linux OS Runs Out of Memory?

It's an important question, as running out of memory can cause your system to slow down or even crash.

Computers need memory to run programs and store data. Linux, the popular open-source operating system, uses two types of memory - RAM (random access memory) and swap space. RAM is the fast, primary memory that programs use directly. Swap space is slower disk-based memory that Linux can use when RAM fills up.

When a Linux system starts to run low on RAM, it will start using the swap space. This allows the system to continue running, but things start to slow down significantly. Programs take longer to respond and the overall system performance degrades.

But what happens when both the RAM and swap space are completely full?

This is when the Linux kernel, the core of the operating system, steps in to prevent a complete system crash.

The kernel has a special mechanism called the Out-of-Memory (OOM) killer. When the system is completely out of memory, the OOM killer identifies the process that is consuming the most memory and terminates it. This frees up memory and allows the system to continue running, albeit in a degraded state.

OOM Killer Linux

The OOM killer uses a special formula to select the "victim" process. It looks at how much virtual memory the process is using, as well as how long the process has been running. The goal is to find a process that uses a lot of memory but hasn't been running for a very long time.

Terminating the most memory-hungry process, the OOM killer can often save the rest of the system from crashing. Of course, this is not an ideal situation - the termination process will be lost, which could be disruptive. But it's a last-resort mechanism to prevent the entire system from failing.

OOM Kernel documentation

Top comments (0)