How can I get the memory usage of a worker created by worker_threads in nodejs

5.1k Views Asked by At

I created a worker using the worker_threads. Like this:

const WorkerThreads = require('worker_threads');
const worker = new WorkerThreads.Worker('./path/to/a.js');

if (countMemory(worker) > 100 * 1024 * 1024) {
    worker.terminate();
}

function countMemory(worker) {
    // some code I don't know
}

And I knew that the ./path/to/a.js has a little bit memory leak, so I have to destory the Worker when it leak a lot. How can I get how many memory did the Worker use?

2

There are 2 best solutions below

1
On

You can use process.memoryUsage to get the info from all the threads. Check this question. I think you can use the same approach for worker_threads

3
On

See the resourceLimits option you can pass to the Worker constructor. Should be more useful than manually checking for memory usage in your use case.

https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options

resource limits An optional set of resource limits for the new JS engine instance. Reaching these limits will lead to termination of the Worker instance. These limits only affect the JS engine, and no external data, including no ArrayBuffers. Even if these limits are set, the process may still abort if it encounters a global out-of-memory situation.

  • maxOldGenerationSizeMb The maximum size of the main heap in MB.
  • maxYoungGenerationSizeMb The maximum size of a heap space for recently created objects.
  • codeRangeSizeMb The size of a pre-allocated memory range used for generated code.
  • stackSizeMb The default maximum stack size for the thread. Small values may lead to unusable Worker instances. Default: 4.

Worker also has getHeapSnapshot() method exposed. As Worker has its own v8 instance running so you can use all v8 API to get more details.