Browse Source

Fixed memory consumption of caches (#7064)

Thanks to @aleos89
Lemongrass3110 2 years ago
parent
commit
df9ef6fa79
1 changed files with 10 additions and 2 deletions
  1. 10 2
      src/common/database.hpp

+ 10 - 2
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;
+			}
+		}
 	}
 };