Home > Backend Development > Python Tutorial > Why Does NumPy Fail to Allocate Large Arrays on Ubuntu, and How Can I Fix It?

Why Does NumPy Fail to Allocate Large Arrays on Ubuntu, and How Can I Fix It?

Patricia Arquette
Release: 2024-11-30 14:40:11
Original
143 people have browsed it

Why Does NumPy Fail to Allocate Large Arrays on Ubuntu, and How Can I Fix It?

Unable to Allocate Array with Shape and Data Type

Symptom

When allocating large arrays in NumPy on Ubuntu 18, users may encounter a "MemoryError: Unable to allocate array with shape" error. This occurs despite sufficient system memory. Notably, no such issue arises on macOS.

Cause

The issue stems from the system's overcommit handling mode. In the default mode (0), the kernel may reject allocation requests deemed too excessive for physical memory.

Resolution

To resolve the error:

  1. Check the overcommit mode:

    $ cat /proc/sys/vm/overcommit_memory
    Copy after login

    If it returns 0, proceed to step 2.

  2. Enable "always overcommit" mode as root:

    $ echo 1 > /proc/sys/vm/overcommit_memory
    Copy after login
  3. Retry the array allocation.

Example

For an array of dimensions (156816, 36, 53806), with a uint8 data type:

import numpy as np

# Allocate array with "always overcommit" mode enabled
a = np.zeros((156816, 36, 53806), dtype='uint8')
print(a.nbytes)  # 303755101056 bytes
Copy after login

The system will allocate memory only when specific array elements are written to. This allows for the use of sparse arrays.

The above is the detailed content of Why Does NumPy Fail to Allocate Large Arrays on Ubuntu, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template