From 149d8176b0ff0b1fc848bca46ab8bca2079b7ab8 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 9 Feb 2014 01:34:08 -0600 Subject: [PATCH] Fix an issue causing excessive memory consumption. This commit resolves an issue where the block node index was forcing entire blocks to be kept in memory thereby forcing excessive memory usage. For example, prior to this change, the memory usage could consume upwards of 1.5GB while importing bootstrap.dat via the addblock utility. With this change the entire import takes <150MB. This also has the same memory reduction to btcd since it uses the same code path. --- chain.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/chain.go b/chain.go index 575b94c4b..70e527ee1 100644 --- a/chain.go +++ b/chain.go @@ -78,9 +78,13 @@ type blockNode struct { // for the passed block. The work sum is updated accordingly when the node is // inserted into a chain. func newBlockNode(blockHeader *btcwire.BlockHeader, blockSha *btcwire.ShaHash, height int64) *blockNode { + // Make a copy of the hash so the node doesn't keep a reference to part + // of the full block/block header preventing it from being garbage + // collected. + prevHash := blockHeader.PrevBlock node := blockNode{ hash: blockSha, - parentHash: &blockHeader.PrevBlock, + parentHash: &prevHash, workSum: CalcWork(blockHeader.Bits), height: height, version: blockHeader.Version,