cmd/utils, cmd/geth: add --memorylimit flag

This commit is contained in:
jonny rhea 2026-07-09 16:58:39 -05:00
parent ca3a72e8c2
commit c04540a2db
2 changed files with 15 additions and 4 deletions

View file

@ -101,6 +101,7 @@ var (
utils.CachePreimagesFlag, utils.CachePreimagesFlag,
utils.CacheLogSizeFlag, utils.CacheLogSizeFlag,
utils.FDLimitFlag, utils.FDLimitFlag,
utils.MemoryLimitFlag,
utils.CryptoKZGFlag, utils.CryptoKZGFlag,
utils.ListenPortFlag, utils.ListenPortFlag,
utils.DiscoveryPortFlag, utils.DiscoveryPortFlag,

View file

@ -554,6 +554,11 @@ var (
Usage: "Raise the open file descriptor resource limit (default = system fd limit)", Usage: "Raise the open file descriptor resource limit (default = system fd limit)",
Category: flags.PerfCategory, Category: flags.PerfCategory,
} }
MemoryLimitFlag = &cli.IntFlag{
Name: "memorylimit",
Usage: "Soft memory limit for the Go runtime in megabytes (default = derived from --cache and available memory)",
Category: flags.PerfCategory,
}
CryptoKZGFlag = &cli.StringFlag{ CryptoKZGFlag = &cli.StringFlag{
Name: "crypto.kzg", Name: "crypto.kzg",
Usage: "KZG library implementation to use; gokzg (recommended) or ckzg", Usage: "KZG library implementation to use; gokzg (recommended) or ckzg",
@ -1776,11 +1781,16 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
// memory below a soft cap. We size the cap from the same cgroup-aware // memory below a soft cap. We size the cap from the same cgroup-aware
// limit used for the cache sanitizer above (total), so it's large enough // limit used for the cache sanitizer above (total), so it's large enough
// that GOGC=100 governs steady state but the limit engages under real // that GOGC=100 governs steady state but the limit engages under real
// memory pressure. When the effective limit can't be determined, leave // memory pressure. An explicit --memorylimit overrides this and when neither
// Go's defaults untouched. // is available, Go's defaults are left untouched.
if total > 0 { if ctx.IsSet(MemoryLimitFlag.Name) {
memLimit := int64(ctx.Int(MemoryLimitFlag.Name)) * 1024 * 1024
log.Debug("Setting Go memory limit", "MB", memLimit/1024/1024, "source", "flag")
godebug.SetMemoryLimit(memLimit)
godebug.SetGCPercent(100)
} else if total > 0 {
cache := int64(ctx.Int(CacheFlag.Name)) * 1024 * 1024 cache := int64(ctx.Int(CacheFlag.Name)) * 1024 * 1024
// Target ~4× the cache so GOGC=100 governs steady state, but cap at // Target ~4x the cache so GOGC=100 governs steady state, but cap at
// half of memory. The rest is for the OS, page cache, and off-heap // half of memory. The rest is for the OS, page cache, and off-heap
// caches like Pebble that SetMemoryLimit doesn't count. // caches like Pebble that SetMemoryLimit doesn't count.
memLimit := min(int64(total)/2, cache*4) memLimit := min(int64(total)/2, cache*4)