Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
Nodejs worker_threads Memory leak

I am using the following Code, to perform a cost intensive calculation using multiple Threads in NodeJS:

Worker:

import { workerData, parentPort } from 'worker_threads';
let digits = workerData as number[];
let digitCount = digits.length;

// Worker Code
parentPort.on('message', (msg) => {
    let [start, end] = msg;
    for(let i = start; i < end; i++) {
        let res = calculate(digits, digitCount, msg);
        if(res) {
            parentPort.postMessage(res);
        }
    }
    parentPort.postMessage(true);
    process.exit(0);
});

Main:

import { Worker } from 'worker_threads';
import { cpus } from 'os';

export class WorkerPool {
    private workers: Worker[];
    private resolve: (value: void | PromiseLike<void>) => void;
    private done: number;

    constructor(digits: number[], occurances: Map<number, number | false>, private count: number = (cpus().length - 1)) {
        if(count < 1) count = 1;
        this.workers = new Array<Worker>(count);

        for(let i = 0; i < count; i++) {
            let worker: Worker = new Worker(require.resolve('./worker'), {
                workerData: digits,
            }) as Worker;

            this.workers[i] = worker;

            worker.on('message', (val: number | true) => {
                if(val === true) {
                    if(++this.done >= count) {
                        this.resolve();
                    }
                    return;
                } 

                let idx = occurances.get(val);
                if(idx === undefined) {
                    occurances.set(val, i);
                } else if(idx !== false) {
                    occurances.set(val, false);
                }

            });
        }
    }

    calculate(maxi: number) {
        return new Promise<void>((resolve, reject) => {
            this.resolve = resolve;
            let d = Math.floor(maxi / this.count);
            let r = maxi % this.count;

            let start = 0;
            for(let i = 0; i < this.count; i++) {
                let end = start + d + (i < r ? 1 : 0);
                this.workers[i].postMessage([start, end]);
                start = end;
            }
        });
    }
}

The function calculate is just a relatively simple function, however it is run around 1 billion times. That is why i tried to use multithreading. However, when i do

await pool.calculate(1000000000)

the calculation does not finish and the Ram usage goes to 64GB. When running the calculate function in the main Thread, it works as expected, so i guess it's a fault in my WorkerPool class.

about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda