From d5aec7ba48c395db3386172b892e334bc55e44d3 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Mon, 13 Apr 2015 20:53:43 +0200 Subject: Issue #21116: Avoid blowing memory when allocating a multiprocessing shared array that's larger than 50% of the available RAM. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Patch by Médéric Boquien. --- Lib/multiprocessing/heap.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'Lib/multiprocessing/heap.py') diff --git a/Lib/multiprocessing/heap.py b/Lib/multiprocessing/heap.py index 333b3baf20..44d9638ff6 100644 --- a/Lib/multiprocessing/heap.py +++ b/Lib/multiprocessing/heap.py @@ -71,7 +71,14 @@ else: os.unlink(name) util.Finalize(self, os.close, (self.fd,)) with open(self.fd, 'wb', closefd=False) as f: - f.write(b'\0'*size) + bs = 1024 * 1024 + if size >= bs: + zeros = b'\0' * bs + for _ in range(size // bs): + f.write(zeros) + del zeros + f.write(b'\0' * (size % bs)) + assert f.tell() == size self.buffer = mmap.mmap(self.fd, self.size) def reduce_arena(a): -- cgit v1.2.1