From c3f28518726a1ffe0051282f66666abb88710d18 Mon Sep 17 00:00:00 2001 From: Sina M <1591639+s1na@users.noreply.github.com> Date: Tue, 14 Jul 2026 20:00:47 +0200 Subject: [PATCH] cmd/utils: tune GOGC for large cache values (#34851) --------- Co-authored-by: jonny rhea <5555162+jrhea@users.noreply.github.com> --- cmd/geth/main.go | 1 + cmd/utils/flags.go | 16 +++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 0108867f5a..b5802f51e7 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -101,6 +101,7 @@ var ( utils.CachePreimagesFlag, utils.CacheLogSizeFlag, utils.FDLimitFlag, + utils.MemoryLimitFlag, utils.CryptoKZGFlag, utils.ListenPortFlag, utils.DiscoveryPortFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 4082717930..8b06772d87 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -554,6 +554,11 @@ var ( Usage: "Raise the open file descriptor resource limit (default = system fd limit)", Category: flags.PerfCategory, } + MemoryLimitFlag = &cli.IntFlag{ + Name: "memorylimit", + Usage: "Soft memory limit for the Go runtime in megabytes (default = no limit)", + Category: flags.PerfCategory, + } CryptoKZGFlag = &cli.StringFlag{ Name: "crypto.kzg", Usage: "KZG library implementation to use; gokzg (recommended) or ckzg", @@ -1762,12 +1767,13 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { ctx.Set(CacheFlag.Name, strconv.Itoa(allowance)) } } - // Ensure Go's GC ignores the database cache for trigger percentage - cache := ctx.Int(CacheFlag.Name) - gogc := max(20, min(100, 100/(float64(cache)/1024))) - log.Debug("Sanitizing Go's GC trigger", "percent", int(gogc)) - godebug.SetGCPercent(int(gogc)) + godebug.SetGCPercent(100) + if ctx.IsSet(MemoryLimitFlag.Name) { + memLimit := int64(ctx.Int(MemoryLimitFlag.Name)) * 1024 * 1024 + log.Debug("Setting Go memory limit", "MB", memLimit/1024/1024) + godebug.SetMemoryLimit(memLimit) + } if ctx.IsSet(SyncTargetFlag.Name) { cfg.SyncMode = ethconfig.FullSync // dev sync target forces full sync