From df9ef6fa79ba2f6fa8817033ad2cd74e3c0de6f2 Mon Sep 17 00:00:00 2001 From: Lemongrass3110 Date: Thu, 18 Aug 2022 00:06:47 +0200 Subject: [PATCH] Fixed memory consumption of caches (#7064) Thanks to @aleos89 --- src/common/database.hpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/common/database.hpp b/src/common/database.hpp index 51af27df10..6ef08d83d7 100644 --- a/src/common/database.hpp +++ b/src/common/database.hpp @@ -196,8 +196,16 @@ public: this->cache[key] = pair.second; } - // Free the memory that was allocated too much - this->cache.shrink_to_fit(); + for( auto it = this->cache.rbegin(); it != this->cache.rend(); it++ ){ + if( *it != nullptr ){ + // Resize to only fit all existing non null entries + this->cache.resize( this->cache.rend() - it ); + + // Free the memory that was allocated too much + this->cache.shrink_to_fit(); + break; + } + } } };