Skip to content

Commit 5e4ac5a

Browse files
committed
Call ReallocateCache() on each Flush()
This frees up all associated memory with the map, not only the nodes. This is necessary in preparation for using the PoolAllocator for CCoinsMap, which does not actually free any memory on clear().
1 parent 1afca6b commit 5e4ac5a

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

src/coins.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,12 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
253253

254254
bool CCoinsViewCache::Flush() {
255255
bool fOk = base->BatchWrite(cacheCoins, hashBlock, /*erase=*/true);
256-
if (fOk && !cacheCoins.empty()) {
257-
/* BatchWrite must erase all cacheCoins elements when erase=true. */
258-
throw std::logic_error("Not all cached coins were erased");
256+
if (fOk) {
257+
if (!cacheCoins.empty()) {
258+
/* BatchWrite must erase all cacheCoins elements when erase=true. */
259+
throw std::logic_error("Not all cached coins were erased");
260+
}
261+
ReallocateCache();
259262
}
260263
cachedCoinsUsage = 0;
261264
return fOk;

src/test/validation_flush_tests.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
131131
CoinsCacheSizeState::OK);
132132
}
133133

134-
// Flushing the view doesn't take us back to OK because cacheCoins has
135-
// preallocated memory that doesn't get reclaimed even after flush.
134+
// Flushing the view does take us back to OK because ReallocateCache() is called
136135

137136
BOOST_CHECK_EQUAL(
138137
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
@@ -144,7 +143,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
144143

145144
BOOST_CHECK_EQUAL(
146145
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
147-
CoinsCacheSizeState::CRITICAL);
146+
CoinsCacheSizeState::OK);
148147
}
149148

150149
BOOST_AUTO_TEST_SUITE_END()

src/validation.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4930,7 +4930,6 @@ bool Chainstate::ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
49304930
} else {
49314931
// Otherwise, flush state to disk and deallocate the in-memory coins map.
49324932
ret = FlushStateToDisk(state, FlushStateMode::ALWAYS);
4933-
CoinsTip().ReallocateCache();
49344933
}
49354934
return ret;
49364935
}

0 commit comments

Comments
 (0)