Skip to content

Commit

Permalink
Merge pull request #264 from OffchainLabs/fix-state-at-block-memory-leak
Browse files Browse the repository at this point in the history
fix memory leak in StateAtBlock
  • Loading branch information
joshuacolvin0 authored Oct 31, 2023
2 parents 859182f + c4f5293 commit 4bdccdd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions eth/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (eth *Ethereum) StateAtBlock(ctx context.Context, block *types.Block, reexe
// Create an ephemeral trie.Database for isolating the live one. Otherwise
// the internal junks created by tracing will be persisted into the disk.
database = state.NewDatabaseWithConfig(eth.chainDb, &trie.Config{Cache: 16})
defer database.TrieDB().ResetCleans()
if statedb, err = state.New(block.Root(), database, nil); err == nil {
log.Info("Found disk backend for state trie", "root", block.Root(), "number", block.Number())
return statedb, noopReleaser, nil
Expand All @@ -100,6 +101,7 @@ func (eth *Ethereum) StateAtBlock(ctx context.Context, block *types.Block, reexe
// Create an ephemeral trie.Database for isolating the live one. Otherwise
// the internal junks created by tracing will be persisted into the disk.
database = state.NewDatabaseWithConfig(eth.chainDb, &trie.Config{Cache: 16})
defer database.TrieDB().ResetCleans()

// If we didn't check the live database, do check state over ephemeral database,
// otherwise we would rewind past a persisted block (specific corner case is
Expand Down
8 changes: 8 additions & 0 deletions trie/database_wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func prepare(diskdb ethdb.Database, config *Config) *Database {
} else {
cleans = fastcache.LoadFromFileOrNew(config.Journal, config.Cache*1024*1024)
}
runtime.SetFinalizer(cleans, func(c *fastcache.Cache) { c.Reset() })
}
var preimages *preimageStore
if config != nil && config.Preimages {
Expand All @@ -97,6 +98,13 @@ func prepare(diskdb ethdb.Database, config *Config) *Database {
}
}

// resets fastcache to return memory chunks to pool of free chunks
func (db *Database) ResetCleans() {
if db.cleans != nil {
db.cleans.Reset()
}
}

// NewDatabase initializes the trie database with default settings, namely
// the legacy hash-based scheme is used by default.
func NewDatabase(diskdb ethdb.Database) *Database {
Expand Down

0 comments on commit 4bdccdd

Please sign in to comment.