1

I am writing a Lichess bot using JavaScript (Node), which analyses chess positions using Stockfish.js (https://www.npmjs.com/package/stockfish). Since multiple games can be played by bots simultaneously, I figured it would make sense for each game to have its own instance of Stockfish, which should be deallocated once the game is over.

However, deallocating the memory has been problematic. I get memory leaks of up to 80MB per game, even after sending stop and quit UCI commands, and also trying approaches like this.worker = null or delete this.worker. Nothing works.

Code snippets:

  1. Engine initialisation is done in constructor.
constructor() {
  this.worker = stockfish();
  this.worker.onmessage = data => {
    console.log(data);
  };
  this.worker.postMessage("uci");
}
  1. Stopping the engine.
stop() {
  this.worker.postMessage("stop");
  this.worker.postMessage("quit");
  setTimeout(() => {
    this.worker = null;
    console.log("Engine stopped");
  }, 1500);
}

I use setTimeout() when testing this.worker = null or delete this.worker because the statements seem to affect the previous statements when not delayed. I'm still investigating this.

Any ideas what I can do regarding the leaks? Thank you.

Victor Durojaiye
  • 162
  • 1
  • 10
  • Couldn't fix this problem then so I ended up deploying the native Stockfish binary along with my app, then spawning a process in Node and killing it when it is no longer needed. I had better control this way, plus native Stockfish is much stronger anyway. – Victor Durojaiye May 07 '22 at 21:36

0 Answers0