Oserror: [errno 12] cannot allocate memory

Hello coders, today we are going to examine the rather tricky Python error: “oserror: [errno 12] cannot allocate memory“. We will explore its causes and solutions to this pesky issue.

If you are dealing with Python programming, chances are high that you may come across this error and it can be quite a headache if you’re not familiar with its root causes and potential solutions.

What is the “oserror: [errno 12] cannot allocate memory”?

Simply put, “oserror: [errno 12] cannot allocate memory” pops up when there’s not enough memory space to execute a task. This occurs when your system runs out of both physical and swap memory. It could be due to improper memory management, an unfavorable overcommit policy, or other related memory allocation issues.

What causes the “oserror: [errno 12] cannot allocate memory” error?

The most common cause is improper memory management or high memory-consuming operations. It often happens when you make consecutive subprocess.Popen calls in Python or when a large amount of memory footprint is being used. Other causes involve overcommit enforcement issues and the lack of control over swap and overcommit configuration.

During my coding life, I learned that Python’s subprocess.Popen uses fork/clone under the hood. This means that every time you call it, you are requesting double the memory that Python is already using. Therefore, if the system lacks an available memory pool, you’ll soon encounter the ENOMEM error.

Codes Examples

Let’s see how we could motivate the error:

import subprocess
import time

for i in range(10000):
    subprocess.Popen('ls', shell=True)

You’ll most likely see our error once you have exceeded your available memory.

How to resolve the “oserror: [errno 12] cannot allocate memory” error?

Solution 1:

You can tackle this issue in two ways:

  • Switch to a larger instance if this option is available, or,
  • Work on your script to control its memory footprint.

You need to replace multiple subprocess.Popen calls with alternatives like vfork, posix_spawn, or even rewriting your code to make it less memory-demanding.

import os
import time

for i in range(10000):

This code should execute without raising an error because vfork consumes less memory than subprocess.Popen.

Solution 2:

Another approach would be to enable swap memory:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k
sudo mkswap /swapfile
sudo swapon /swapfile

And then add this to the fstab to make the swap permanent.

/swapfile none swap sw 0 0 

Solution 3:

If you’re pretty sure that your system should have enough memory, you can bypass the overcommit check with:

echo 1 > /proc/sys/vm/overcommit_memory

Solution 4:

Compare your /proc/sys/vm settings and manage your swap space or use settings to lower the system’s tendency to utilize swap.


1. What is “oserror: [errno 12] cannot allocate memory” error?

It occurs when your system fails to allocate memory for a task.

2. What causes the “oserror: [errno 12] cannot allocate memory” error?

Improper memory management or memory-heavy operations can cause this error.

3. Is the memory allocation error permanent?

No, this error can be fixed by implementing several approaches such as enabling swap memory or lowering the number of subprocess requests.


Dealing with the “oserror: [errno 12] cannot allocate memory” error can be tricky. However, with the right debugging approach and clear understanding of how memory allocation works, a solution can be found. Happy coding!

Remember, every obstacle is a new feature that you are going to decode! Keep coding and exploring.


  1. Memory Management in Python

Follow us at PythonClear to learn more about solutions to general errors one may encounter while programming in Python.

Leave a Comment