Skip to content

Commit 5fc6a77

Browse files
committed
Merge #10133: Clean up calculations of pcoinsTip memory usage
1b55e07 Make threshold for flushing more conservative. (Alex Morcos) f33afd3 Lower default memory footprint slightly (Alex Morcos) 5b95a19 Make pcoinsTip memory calculations consistent (Alex Morcos) Tree-SHA512: d0061138596cf89008397b8729d9b25293938b1ad454cc99a6fe2f6210e94f76dfa78a8f0fce4c1ba3efec4e742a9c1a3ab26676a4a8346d3e7c3055d032669b
2 parents fadf078 + 1b55e07 commit 5fc6a77

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/txdb.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ class CBlockIndex;
2121
class CCoinsViewDBCursor;
2222
class uint256;
2323

24+
//! Compensate for extra memory peak (x1.5-x1.9) at flush time.
25+
static constexpr int DB_PEAK_USAGE_FACTOR = 2;
26+
//! No need to periodic flush if at least this much space still available.
27+
static constexpr int MAX_BLOCK_COINSDB_USAGE = 200 * DB_PEAK_USAGE_FACTOR;
28+
//! Always periodic flush if less than this much space still available.
29+
static constexpr int MIN_BLOCK_COINSDB_USAGE = 50 * DB_PEAK_USAGE_FACTOR;
2430
//! -dbcache default (MiB)
25-
static const int64_t nDefaultDbCache = 300;
31+
static const int64_t nDefaultDbCache = 450;
2632
//! max. -dbcache (MiB)
2733
static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
2834
//! min. -dbcache (MiB)

src/validation.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,10 +2006,11 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode, int n
20062006
nLastSetChain = nNow;
20072007
}
20082008
int64_t nMempoolSizeMax = GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
2009-
int64_t cacheSize = pcoinsTip->DynamicMemoryUsage() * 2; // Compensate for extra memory peak (x1.5-x1.9) at flush time.
2009+
int64_t cacheSize = pcoinsTip->DynamicMemoryUsage() * DB_PEAK_USAGE_FACTOR;
20102010
int64_t nTotalSpace = nCoinCacheUsage + std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0);
2011-
// The cache is large and we're within 10% and 100 MiB of the limit, but we have time now (not in the middle of a block processing).
2012-
bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize > std::max((9 * nTotalSpace) / 10, nTotalSpace - 100 * 1024 * 1024);
2011+
// The cache is large and we're within 10% and 200 MiB or 50% and 50MiB of the limit, but we have time now (not in the middle of a block processing).
2012+
bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize > std::min(std::max(nTotalSpace / 2, nTotalSpace - MIN_BLOCK_COINSDB_USAGE * 1024 * 1024),
2013+
std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE * 1024 * 1024));
20132014
// The cache is over the limit, we have to write now.
20142015
bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nTotalSpace;
20152016
// It's been a while since we wrote the block index to disk. Do this frequently, so we don't need to redownload after a crash.

0 commit comments

Comments
 (0)