Skip to content

Commit 1a01c95

Browse files
committed
Make lowmem threshold configurable
1 parent 71eae9f commit 1a01c95

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/common/system.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,21 @@ int64_t GetStartupTime()
115115
return nStartupTime;
116116
}
117117

118+
size_t g_low_memory_threshold = 10 * 1024 * 1024 /* 10 MB */;
119+
118120
bool SystemNeedsMemoryReleased()
119121
{
120-
constexpr size_t low_memory_threshold = 10 * 1024 * 1024 /* 10 MB */;
122+
if (g_low_memory_threshold <= 0) {
123+
// Intentionally bypass other metrics when disabled entirely
124+
return false;
125+
}
121126
#ifdef WIN32
122127
MEMORYSTATUSEX mem_status;
123128
mem_status.dwLength = sizeof(mem_status);
124129
if (GlobalMemoryStatusEx(&mem_status)) {
125130
if (mem_status.dwMemoryLoad >= 99 ||
126-
mem_status.ullAvailPhys < low_memory_threshold ||
127-
mem_status.ullAvailVirtual < low_memory_threshold) {
131+
mem_status.ullAvailPhys < g_low_memory_threshold ||
132+
mem_status.ullAvailVirtual < g_low_memory_threshold) {
128133
LogPrintf("%s: YES: %s%% memory load; %s available physical memory; %s available virtual memory\n", __func__, int(mem_status.dwMemoryLoad), size_t(mem_status.ullAvailPhys), size_t(mem_status.ullAvailVirtual));
129134
return true;
130135
}
@@ -136,7 +141,7 @@ bool SystemNeedsMemoryReleased()
136141
// Explicitly 64-bit in case of 32-bit userspace on 64-bit kernel
137142
const uint64_t free_ram = uint64_t(sys_info.freeram) * sys_info.mem_unit;
138143
const uint64_t buffer_ram = uint64_t(sys_info.bufferram) * sys_info.mem_unit;
139-
if (free_ram + buffer_ram < low_memory_threshold) {
144+
if (free_ram + buffer_ram < g_low_memory_threshold) {
140145
LogPrintf("%s: YES: %s free RAM + %s buffer RAM\n", __func__, free_ram, buffer_ram);
141146
return true;
142147
}

src/common/system.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ std::string ShellEscape(const std::string& arg);
2525
void runCommand(const std::string& strCommand);
2626
#endif
2727

28+
extern size_t g_low_memory_threshold;
29+
2830
bool SystemNeedsMemoryReleased();
2931

3032
/**

src/init.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ void SetupServerArgs(ArgsManager& argsman)
482482
argsman.AddArg("-includeconf=<file>", "Specify additional configuration file, relative to the -datadir path (only useable from configuration file, not command line)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
483483
argsman.AddArg("-allowignoredconf", strprintf("For backwards compatibility, treat an unused %s file in the datadir as a warning, not an error.", BITCOIN_CONF_FILENAME), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
484484
argsman.AddArg("-loadblock=<file>", "Imports blocks from external file on startup", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
485+
argsman.AddArg("-lowmem=<n>", strprintf("If system available memory falls below <n> MiB, flush caches (0 to disable, default: %s)", g_low_memory_threshold / 1024 / 1024), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
485486
argsman.AddArg("-maxmempool=<n>", strprintf("Keep the transaction memory pool below <n> megabytes (default: %u)", DEFAULT_MAX_MEMPOOL_SIZE_MB), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
486487
argsman.AddArg("-maxorphantx=<n>", strprintf("Keep at most <n> unconnectable transactions in memory (default: %u)", DEFAULT_MAX_ORPHAN_TRANSACTIONS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
487488
argsman.AddArg("-mempoolexpiry=<n>", strprintf("Do not keep transactions in the mempool longer than <n> hours (default: %u)", DEFAULT_MEMPOOL_EXPIRY_HOURS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
@@ -1491,6 +1492,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14911492
}
14921493
LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", cache_sizes.coins * (1.0 / 1024 / 1024), mempool_opts.max_size_bytes * (1.0 / 1024 / 1024));
14931494

1495+
if (gArgs.IsArgSet("-lowmem")) {
1496+
g_low_memory_threshold = gArgs.GetIntArg("-lowmem", 0 /* not used */) * 1024 * 1024;
1497+
}
1498+
if (g_low_memory_threshold > 0) {
1499+
LogPrintf("* Flushing caches if available system memory drops below %s MiB\n", g_low_memory_threshold / 1024 / 1024);
1500+
}
1501+
14941502
for (bool fLoaded = false; !fLoaded && !ShutdownRequested(node);) {
14951503
node.mempool = std::make_unique<CTxMemPool>(mempool_opts);
14961504

0 commit comments

Comments
 (0)