Repository: bitcoin
Issue: Running out of memory on a 2GB box - Initializing chainstate Chainstate [ibd] @ height -1 (null)
Labels: UTXO Db and Indexes,Resource usage

I have a server running a pruned bitcoind node as a Docker container with 2G RAM. There are some other services using maybe 500MB of the RAM but I got into a state where bitcoind tries to initialize the chainstate but then starts sucking up all the available RAM so it gets killed with OOM error. Docker then restarts it and it loops like this forever

I tried to configure bitcoind to be constrained to low memory but it doesn't seem to have any effect

```
[main]
printtoconsole=1
rpcallowip=::/0
rpcallowip=0.0.0.0/0
prune=550
txindex=0
rpcport=8332
rpcbind=0.0.0.0:8332
dbcache=20
maxmempool=5
maxconnections=32

assumevalid=00000000000000000002923a7456aa3d4adce139cd16550c3ff36d07cc45d251
```

This node did successfully complete IBD at one point and it is unknown exactly why or when it got into this OOM loop. Maybe it has something to do with the assumevalid? In fact the way this docker container was constructed is that on every restart it would get the latest block hash from an esplora server and set that hash to assumevalid in bitcoin.conf before initializing. So I am theorizing that maybe this unusual behavior created some sort of data that needs to be fully loaded into memory before proceeding and so it gets killed.

Below are the logs of a typical startup of bitcoind at this point. I have tried locking the hash of assumevalid and disabled the esplora auto-update logic but it didn't have any effect.

As a last resort I will clear out the chainstate and try to do IBD again but am trying to surgically fix it if possible. Decided to open an issue here in case there is some edge case optimization to prevent bitcoind from having to eat up 1.5GB of memory.

Any suggestions are appreciated and feel free to close if there is nothing actionable and this all just came down to a misuse of `assumevalid`. Thanks!

```
bitcoind-1  | 2024-12-27T15:00:16Z Bitcoin Core version v26.0.0 (release build)
bitcoind-1  | 2024-12-27T15:00:16Z Using the 'sse4(1way),sse41(4way),avx2(8way)' SHA256 implementation
bitcoind-1  | 2024-12-27T15:00:16Z Using RdSeed as an additional entropy source
bitcoind-1  | 2024-12-27T15:00:16Z Using RdRand as an additional entropy source
bitcoind-1  | 2024-12-27T15:00:16Z Default data directory /home/bitcoin/.bitcoin
bitcoind-1  | 2024-12-27T15:00:16Z Using data directory /home/bitcoin/.bitcoin
bitcoind-1  | 2024-12-27T15:00:16Z Config file: /home/bitcoin/.bitcoin/bitcoin.conf
bitcoind-1  | 2024-12-27T15:00:16Z Config file arg: [main] assumevalid="00000000000000000002923a7456aa3d4adce139cd16550c3ff36d07cc45d251"
bitcoind-1  | 2024-12-27T15:00:16Z Config file arg: [main] dbcache="20"
bitcoind-1  | 2024-12-27T15:00:16Z Config file arg: [main] maxconnections="32"
bitcoind-1  | 2024-12-27T15:00:16Z Config file arg: [main] maxmempool="5"
bitcoind-1  | 2024-12-27T15:00:16Z Config file arg: [main] printtoconsole="1"
bitcoind-1  | 2024-12-27T15:00:16Z Config file arg: [main] prune="550"
bitcoind-1  | 2024-12-27T15:00:16Z Config file arg: [main] rpcallowip="::/0"
bitcoind-1  | 2024-12-27T15:00:16Z Config file arg: [main] rpcallowip="0.0.0.0/0"
bitcoind-1  | 2024-12-27T15:00:16Z Config file arg: [main] rpcauth=****
bitcoind-1  | 2024-12-27T15:00:16Z Config file arg: [main] rpcbind="0.0.0.0:8332"
bitcoind-1  | 2024-12-27T15:00:16Z Config file arg: [main] rpcport="8332"
bitcoind-1  | 2024-12-27T15:00:16Z Config file arg: [main] txindex="0"
bitcoind-1  | 2024-12-27T15:00:16Z Using at most 32 automatic connections (1048576 file descriptors available)
bitcoind-1  | 2024-12-27T15:00:16Z Using 16 MiB out of 16 MiB requested for signature cache, able to store 524288 elements
bitcoind-1  | 2024-12-27T15:00:16Z Using 16 MiB out of 16 MiB requested for script execution cache, able to store 524288 elements
bitcoind-1  | 2024-12-27T15:00:16Z Script verification uses 0 additional threads
bitcoind-1  | 2024-12-27T15:00:16Z Binding RPC on address 0.0.0.0 port 8332
bitcoind-1  | 2024-12-27T15:00:16Z WARNING: the RPC server is not safe to expose to untrusted networks such as the public internet
bitcoind-1  | 2024-12-27T15:00:16Z [http] creating work queue of depth 16
bitcoind-1  | 2024-12-27T15:00:16Z Using random cookie authentication.
bitcoind-1  | 2024-12-27T15:00:16Z scheduler thread start
bitcoind-1  | 2024-12-27T15:00:16Z Generated RPC authentication cookie /home/bitcoin/.bitcoin/.cookie
bitcoind-1  | 2024-12-27T15:00:16Z Using rpcauth authentication.
bitcoind-1  | 2024-12-27T15:00:16Z [http] starting 4 worker threads
bitcoind-1  | 2024-12-27T15:00:16Z Using wallet directory /home/bitcoin/.bitcoin
bitcoind-1  | 2024-12-27T15:00:16Z init message: Verifying wallet(s)…
bitcoind-1  | 2024-12-27T15:00:16Z Using /16 prefix for IP bucketing
bitcoind-1  | 2024-12-27T15:00:16Z init message: Loading P2P addresses…
bitcoind-1  | 2024-12-27T15:00:17Z Loaded 64073 addresses from peers.dat  284ms
bitcoind-1  | 2024-12-27T15:00:17Z init message: Loading banlist…
bitcoind-1  | 2024-12-27T15:00:17Z SetNetworkActive: true
bitcoind-1  | 2024-12-27T15:00:17Z Cache configuration:
bitcoind-1  | 2024-12-27T15:00:17Z * Using 2.0 MiB for block index database
bitcoind-1  | 2024-12-27T15:00:17Z * Using 8.0 MiB for chain state database
bitcoind-1  | 2024-12-27T15:00:17Z * Using 10.0 MiB for in-memory UTXO set (plus up to 4.8 MiB of unused mempool space)
bitcoind-1  | 2024-12-27T15:00:17Z init message: Loading block index…
bitcoind-1  | 2024-12-27T15:00:17Z Assuming ancestors of block 00000000000000000002923a7456aa3d4adce139cd16550c3ff36d07cc45d251 have valid signatures.
bitcoind-1  | 2024-12-27T15:00:17Z Setting nMinimumChainWork=000000000000000000000000000000000000000052b2559353df4117b7348b64
bitcoind-1  | 2024-12-27T15:00:17Z Prune configured to target 550 MiB on disk for block and undo files.
bitcoind-1  | 2024-12-27T15:00:17Z Opening LevelDB in /home/bitcoin/.bitcoin/blocks/index
bitcoind-1  | 2024-12-27T15:00:17Z Opened LevelDB successfully
bitcoind-1  | 2024-12-27T15:00:17Z Using obfuscation key for /home/bitcoin/.bitcoin/blocks/index: 0000000000000000
bitcoind-1  | 2024-12-27T15:00:22Z LoadBlockIndexDB: last block file = 4657
bitcoind-1  | 2024-12-27T15:00:22Z LoadBlockIndexDB: last block file info: CBlockFileInfo(blocks=25, size=39362654, heights=874318...874342, time=2024-12-11...2024-12-12)
bitcoind-1  | 2024-12-27T15:00:22Z Checking all blk files are present...
bitcoind-1  | 2024-12-27T15:00:22Z LoadBlockIndexDB(): Block files have previously been pruned
bitcoind-1  | 2024-12-27T15:00:23Z Initializing chainstate Chainstate [ibd] @ height -1 (null)
bitcoind-1  | 2024-12-27T15:00:23Z Opening LevelDB in /home/bitcoin/.bitcoin/chainstate
```