Home > Article > Backend Development > Why Can\'t I Allocate Large NumPy Arrays on Ubuntu, and How Can I Fix It?
Memory Allocation Constraints in Numpy for Large Arrays
Facing an error while attempting to allocate a large numpy array on Ubuntu 18, "Unable to allocate array with shape and data type," this issue is not encountered on MacOS. The reason behind this disparity lies in the system's overcommit handling mode.
Overcommit Handling and Memory Allocation
The overcommit handling mode determines how the system manages memory allocation requests. In the default mode (0), the kernel checks if there's sufficient physical memory to commit to the allocation request. If not, it refuses the allocation. In overcommit mode 1, the kernel always allows allocations, regardless of the available physical memory.
Resolution
To resolve this issue on Ubuntu, you need to enable overcommit mode 1. As root, run the following command:
$ echo 1 > /proc/sys/vm/overcommit_memory
This will change the overcommit handling mode to 1, allowing the allocation of the large numpy array.
Sparse Arrays and Virtual Memory
The overcommit mode 1 can be useful for sparse arrays, where only a small portion of the allocated memory is actually used. This is because the system only commits physical memory to the pages that are explicitly written to, thereby conserving physical memory.
Warning
It's important to note that while overcommit mode 1 allows large allocations, it can lead to potential system instability if the allocated memory exceeds the available physical memory. Use overcommit mode 1 with caution and monitor your system's memory usage closely.
The above is the detailed content of Why Can\'t I Allocate Large NumPy Arrays on Ubuntu, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!