From 195f41bd9f0ab5c585ec6addfd5fce70a00a79c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20=E2=80=9CFrogeye=E2=80=9D=20Preud=27homme?= Date: Wed, 25 Dec 2019 13:03:55 +0100 Subject: [PATCH] Use smaller cache if it cannot allocate --- database.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/database.py b/database.py index 173a34b..5a8bda2 100644 --- a/database.py +++ b/database.py @@ -227,10 +227,19 @@ class Database(Profiler): self.log.warning("Allocating more than 512 MiB of RAM for " "the Ip4 cache is not necessary.") max_cache_width = int(math.log2(max(1, max_size*8))) + allocated = False cache_width = min(2**32, max_cache_width) + while not allocated: + cache_size = 2**cache_width + try: + self.ip4cache = numpy.zeros(cache_size, dtype=numpy.bool) + except MemoryError: + self.log.exception( + "Could not allocate cache. Retrying a smaller one.") + cache_width -= 1 + continue + allocated = True self.ip4cache_shift = 32-cache_width - cache_size = 2**cache_width - self.ip4cache = numpy.zeros(cache_size, dtype=numpy.bool) for _ in self.exec_each_ip4(self._set_ip4cache): pass