From 56ece73d4ead83e7ab5e3a79563f417228e95c84 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 18 Nov 2025 13:14:24 -0400 Subject: [PATCH] worker: add worker.getMemoryUsage() API --- doc/api/worker_threads.md | 27 ++++ lib/internal/worker.js | 14 +++ src/async_wrap.h | 1 + src/env_properties.h | 2 + src/node_worker.cc | 115 ++++++++++++++++++ src/node_worker.h | 1 + test/parallel/test-worker-get-memory-usage.js | 45 +++++++ 7 files changed, 205 insertions(+) create mode 100644 test/parallel/test-worker-get-memory-usage.js diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index b44df3da70a95f..46055d5df67af1 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -1828,6 +1828,33 @@ This method returns a `Promise` that will resolve to an object identical to [`v8 or reject with an [`ERR_WORKER_NOT_RUNNING`][] error if the worker is no longer running. This methods allows the statistics to be observed from outside the actual thread. +### `worker.getMemoryUsage()` + + + +* Returns: {Promise} + +Returns an object mirroring [`process.memoryUsage()`][] but scoped to the +worker's isolate: + +* `rss` {integer} Resident Set Size. This value represents the RSS reported by + the worker thread and may still include memory shared across threads. +* `heapTotal` {integer} Total size of the V8 heap for the worker. +* `heapUsed` {integer} Heap space used by the worker. +* `external` {integer} Memory used by C++ objects bound to JavaScript objects in + the worker. +* `arrayBuffers` {integer} Memory allocated for `ArrayBuffer` and + `SharedArrayBuffer` instances within the worker. + +The returned `Promise` rejects with [`ERR_WORKER_NOT_RUNNING`][] if called after +the worker has stopped. + ### `worker.performance`