Stockfish.js leaks memory

342 views Asked by At

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.

0

There are 0 answers