From c767f825c58cadbd61fb359a75c16af65ace0648 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Thu, 23 Jul 2026 11:07:42 +0800 Subject: [PATCH] cmd: add gogc flag (#35363) This PR adds the CLI flag gogc for twisting the garbage collection target. The default value is chosen as the 50, balancing the performance gain and potential memory peak. --- cmd/geth/main.go | 1 + cmd/utils/flags.go | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 4aadb5fb87..48a9ae583f 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -103,6 +103,7 @@ var ( utils.CacheLogSizeFlag, utils.FDLimitFlag, utils.MemoryLimitFlag, + utils.GOGCFlag, utils.CryptoKZGFlag, utils.ListenPortFlag, utils.DiscoveryPortFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index f264ed58e8..81c408798e 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -566,6 +566,12 @@ var ( Usage: "Soft memory limit for the Go runtime in megabytes (default = no limit)", Category: flags.PerfCategory, } + GOGCFlag = &cli.IntFlag{ + Name: "gogc", + Usage: "Go garbage collection target percentage (default = 50, negative disables)", + Value: 50, + Category: flags.PerfCategory, + } CryptoKZGFlag = &cli.StringFlag{ Name: "crypto.kzg", Usage: "KZG library implementation to use; gokzg (recommended) or ckzg", @@ -1778,11 +1784,25 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { } } - godebug.SetGCPercent(100) + // Setting go runtime settings + gcPercent := ctx.Int(GOGCFlag.Name) + if ctx.IsSet(GOGCFlag.Name) { + log.Info("Sanitizing Go's GC trigger", "percent", gcPercent) + } + godebug.SetGCPercent(gcPercent) + 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 memLimit > int64(total) { + log.Info("Sanitizing memory limit", "provided(MB)", memLimit/1024/1024, "updated(MB)", total/1024/1024) + memLimit = int64(total) + } + if memLimit < 0 { + log.Warn("Ignoring negative Go memory limit") + } else { + log.Info("Setting Go memory limit", "MB", memLimit/1024/1024) + godebug.SetMemoryLimit(memLimit) + } } if ctx.IsSet(SyncTargetFlag.Name) {