Skip to content

Creating a bunch of mmap.mmap objects fails with "OSError: [Errno 12] Cannot allocate memory" (Python 3.13, Linux) #126715

@Azureblade3808

Description

@Azureblade3808

Bug description:

With the help of newly added trackfd=False option in mmap.mmap constructor, we can create far more mmap.mmap objects beyond the open file limit. But creating a lot of mmap.mmap instances seems to cause memory errors (“OSError: [Errno 12] Cannot allocate memory”).

Example:

import mmap, os

fn = "/tmp/some_file"
fd = os.open(fn, os.O_CREAT | os.O_RDWR | os.O_TRUNC, 0o600)
os.ftruncate(fd, 1)

mmap_objects = []
try:
    for i in range(1_000_000):
        mmap_object = mmap.mmap(fd, 1, trackfd=False)
        mmap_objects.append(mmap_object)
except:
    print(i)
    raise

I ran the snippet above on a CentOS8 server with 48GB RAM, and it stopped at the 65479th iterations with the mentioned error.

By contrast, creation of 1M builtins.object instances can be easily done and a C program that creates 1M mmap pointers can also finish with no error.

This issue were also reproduced on RHEL7.2.

CPython versions tested on:

3.13

Operating systems tested on:

Linux (CentOS 8, RHEL 7)

EDIT

I made a horrible mistake in my C program that it does not check if the pointer returned by mmap is valid. In fact, the max number of valid mmap pointers it can create is 65515. Sorry for the false and misleading statement.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions