internal/debug: add debug_setMemoryLimit (#31441)

This commit is contained in:
georgehao 2025-04-22 12:57:17 +08:00 committed by GitHub
parent 263aef9cc4
commit 1591d165c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 0 deletions

View file

@ -35,6 +35,7 @@ import (
"sync"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/hashicorp/go-bexpr"
)
@ -237,6 +238,24 @@ func (*HandlerT) SetGCPercent(v int) int {
return debug.SetGCPercent(v)
}
// SetMemoryLimit sets the GOMEMLIMIT for the process. It returns the previous limit.
// Note:
//
// - The input limit is provided as bytes. A negative input does not adjust the limit
//
// - A zero limit or a limit that's lower than the amount of memory used by the Go
// runtime may cause the garbage collector to run nearly continuously. However,
// the application may still make progress.
//
// - Setting the limit too low will cause Geth to become unresponsive.
//
// - Geth also allocates memory off-heap, particularly for fastCache and Pebble,
// which can be non-trivial (a few gigabytes by default).
func (*HandlerT) SetMemoryLimit(limit int64) int64 {
log.Info("Setting memory limit", "size", common.PrettyDuration(limit))
return debug.SetMemoryLimit(limit)
}
func writeProfile(name, file string) error {
p := pprof.Lookup(name)
log.Info("Writing profile records", "count", p.Count(), "type", name, "dump", file)

View file

@ -269,6 +269,11 @@ web3._extend({
call: 'debug_setGCPercent',
params: 1,
}),
new web3._extend.Method({
name: 'setMemoryLimit',
call: 'debug_setMemoryLimit',
params: 1,
}),
new web3._extend.Method({
name: 'memStats',
call: 'debug_memStats',