Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d5aec7b

Browse files
committed
Issue #21116: Avoid blowing memory when allocating a multiprocessing shared
array that's larger than 50% of the available RAM. Patch by Médéric Boquien.
1 parent 4269d6d commit d5aec7b

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lib/multiprocessing/heap.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ def __init__(self, size, fd=-1):
7171
os.unlink(name)
7272
util.Finalize(self, os.close, (self.fd,))
7373
with open(self.fd, 'wb', closefd=False) as f:
74-
f.write(b'\0'*size)
74+
bs = 1024 * 1024
75+
if size >= bs:
76+
zeros = b'\0' * bs
77+
for _ in range(size // bs):
78+
f.write(zeros)
79+
del zeros
80+
f.write(b'\0' * (size % bs))
81+
assert f.tell() == size
7582
self.buffer = mmap.mmap(self.fd, self.size)
7683

7784
def reduce_arena(a):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ Wouter Bolsterlee
154154
Gawain Bolton
155155
Forest Bond
156156
Gregory Bond
157+
Médéric Boquien
157158
Matias Bordese
158159
Jonas Borgström
159160
Jurjen Bos

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Core and Builtins
2929
Library
3030
-------
3131

32+
- Issue #21116: Avoid blowing memory when allocating a multiprocessing shared
33+
array that's larger than 50% of the available RAM. Patch by Médéric Boquien.
34+
3235
- Issue #22982: Improve BOM handling when seeking to multiple positions of
3336
a writable text file.
3437

0 commit comments

Comments
 (0)