worker: add worker.getMemoryUsage() API#60778
worker: add worker.getMemoryUsage() API#60778geekdroid07 wants to merge 1 commit intonodejs:mainfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #60778 +/- ##
==========================================
- Coverage 88.57% 88.54% -0.04%
==========================================
Files 703 703
Lines 208256 208347 +91
Branches 40158 40174 +16
==========================================
+ Hits 184469 184486 +17
- Misses 15824 15851 +27
- Partials 7963 8010 +47
🚀 New features to boost your workflow:
|
| Number::New(isolate, heap_stats.external_memory()), | ||
| Number::New(isolate, allocator == nullptr | ||
| ? 0 | ||
| : allocator->total_mem_usage()), |
There was a problem hiding this comment.
There's no guarantee that allocator is still alive at this point
| <!-- YAML | ||
| added: | ||
| changes: | ||
| - version: v25.2.0 |
There was a problem hiding this comment.
| - version: v25.2.0 | |
| - version: REPLACEME |
| `SharedArrayBuffer` instances within the worker. | ||
|
|
||
| The returned `Promise` rejects with [`ERR_WORKER_NOT_RUNNING`][] if called after | ||
| the worker has stopped. |
There was a problem hiding this comment.
This should explain the relationship to worker.getHeapStatistics() and how it differs from that
| 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 |
There was a problem hiding this comment.
Isn't RSS a per process thing?
Therefore RSS value should be the same on all workers so quite pointless to read it from a different thread.
| added: | ||
| changes: | ||
| - version: v25.2.0 | ||
| pr-url: https://github.com/nodejs/node/pull/REPLACEME |
There was a problem hiding this comment.
| pr-url: https://github.com/nodejs/node/pull/REPLACEME | |
| pr-url: https://github.com/nodejs/node/pull/60778 |
|
I’m currently 👎 on this direction. I’d really prefer that we don’t further encourage the We already have an open feature request for proper Web Worker support: nodejs/node#43583. I’d much rather see us move toward that goal than continue to add Node-specific methods to If this metric is important, it seems better modeled as: something the worker explicitly reports back, e.g.: import process from 'node:process'
postMessage(process.memoryUsage())which can be accomplish today already. Or something exposed via a Node-specific side-channel on the main thread, e.g. hypothetically: process.memoryUsage(worker)If we ever do have real Web Workers in Node, we definitely wouldn’t want So in summary: I think the right place for this is a Node-specific side-channel (like |
Summary
worker.getMemoryUsage()so worker threads can report RSS, heap usage, external memory, and ArrayBuffer statistics.WorkerMemoryUsageTakerplus the per-isolate templates/bindings to expose this data.test/parallel/test-worker-get-memory-usage.js) and document the API indoc/api/worker_threads.md.Testing
python3 tools/test.py parallel/test-worker-get-memory-usageninja -C out/Debug node