1. Overview

Linux is the operating system of choice for many servers and development environments. However, it’s not immune to errors, one of which is the dreaded “Cannot allocate memory” error. This error typically occurs when the system runs out of available memory (RAM) or swap space. This makes it unable to allocate additional memory for processes.

In this article, we’ll explore the causes of this error and how to fix it.

2. Understanding This Error

Before diving into the solutions, it’s essential to understand what causes this error.

In Linux, memory allocation involves both physical RAM and swap space. When the system’s RAM is full, it starts using swap space, which is a portion of the hard drive designated to act as overflow memory. If both RAM and swap space are exhausted, the system can no longer allocate memory to new or existing processes. This leads to the “Cannot allocate memory” error.

The most common cause is the system simply running out of available memory. This happens if we’re running memory-intensive applications or if the system has limited RAM and swap space.

A poorly written application might have a memory leak, where it consumes memory but never releases it.

Linux, by default, allows memory overcommitment, meaning it permits more memory allocation requests than the actual available memory. This, in turn, leads to situations where the system suddenly cannot fulfill memory requests.

System configuration, such as limits on the maximum amount of memory a process uses, also causes this error.

We may implement the Linux kernel’s demand paging in applications. This allows us to allocate large virtual address spaces as long as we only want to read (zeros). This is applied practically with sparse data structures, whose elements are mostly zero.

3. Check Available Memory and Swap Space

The first step is to check how much memory and swap space is available on the system. We usually do this by using:

$ free -h
               total        used        free      shared  buff/cache   available
Mem:           7.7Gi       4.2Gi       3.4Gi       2.0Mi       134Mi       3.3Gi
Swap:          2.0Gi        73Mi       1.9Gi

This command displays the total, used, and available memory, as well as swap space in a human-readable format.

If we find that the system’s swap space is low or non-existent, we might need to increase it. Here’s how to do it:

$ sudo dd if=/dev/zero of=/swapfile bs=1G count=4
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile

This sequence of commands creates a 4GB swap file and activates it. We may adjust the size as needed.

Let’s make this change permanent by adding the following line to /etc/fstab:

/swapfile none swap sw 0 0

If the problem persists, it might be due to a memory leak in one of our applications. We can use tools like top,* htop*, or ps** to monitor which processes consume the most memory.

The most straightforward solution for persistent memory allocation issues is to consider upgrading the system’s RAM.

4. Adjust Overcommit Settings

Linux allows us to control how it handles memory overcommitment. We modify this behavior usually by adjusting the vm.overcommit_memory setting.

To do so, let’s add or modify the following line in /etc/sysctl.conf file:

vm.overcommit_memory=2

We, then, apply the changes with:

$ sudo sysctl -p

Setting this to 2 will limit memory allocation to available swap plus a configurable percentage of physical RAM.

5. Check and Adjust the ulimit

The ulimit command controls the resources available to the shell and its processes. Let’s view the current limits with:

$ ulimit -a
real-time non-blocking time  (microseconds, -R) unlimited
core file size              (blocks, -c) 0
data seg size               (kbytes, -d) unlimited
scheduling priority                 (-e) 0
file size                   (blocks, -f) unlimited
pending signals                     (-i) 31359
max locked memory           (kbytes, -l) 65536
max memory size             (kbytes, -m) unlimited
open files                          (-n) 1024
pipe size                (512 bytes, -p) 8
POSIX message queues         (bytes, -q) 819200
real-time priority                  (-r) 0
stack size                  (kbytes, -s) 8192
cpu time                   (seconds, -t) unlimited
max user processes                  (-u) 31359
virtual memory              (kbytes, -v) unlimited
file locks                          (-x) unlimited

If we find that the memory limits are too restrictive, we usually increase them using:

$ ulimit -m unlimited

This command sets the maximum memory size to unlimited, allowing processes to use as much memory as available.

6. Conclusion

In this article, we understood that the “Cannot allocate memory” error in Linux is usually a sign that our system is running out of available memory.

We discussed, how to effectively resolve this issue by checking our system’s memory usage, adding swap space, and adjusting overcommit settings. If all else fails, upgrading our system’s RAM may be necessary to handle the workload.

By following the steps outlined in this article, we should be able to fix the “Cannot allocate memory” error and keep our Linux system running smoothly.