keccak icon indicating copy to clipboard operation
keccak copied to clipboard

Memory leakage

Open sadeghte opened this issue 2 years ago • 1 comments

When you run this simple code, memory grows unbounded. In my test it leaked more than 5 GB of memory.

import createKeccakHash from 'keccak'

const hash = msg => {
  return '0x' + createKeccakHash('keccak256').update(msg).digest('hex')
}

const mine = function(seed, difficulty = 5) {
  let nonce = 0;
  difficulty = parseInt(difficulty);
  const prefix = '0x' + new Array(difficulty).fill('0').join('')
  while (true) {
    const h = hash(seed + nonce);
    if (!!h && h.substring(0, difficulty+2) === prefix) {
      return nonce;
    }
    nonce += 1;
  }
}

console.log("running ....")
const seed = "hfjshjdhsjhdjshdjs"
const nonce = mine(seed, 8)
console.log({
  nonce,
})

the createKeccakHash method thakes some memory and not release it.

sadeghte avatar Mar 10 '23 20:03 sadeghte

I noticed this as well. Seems like a problem specific to the node bindings. When importing keccak/js instead, it did not leak.

ericz avatar Mar 22 '23 01:03 ericz