Skip to content

Conversation

@geekdroid07
Copy link

Summary

  • Add worker.getMemoryUsage() so worker threads can report RSS, heap usage, external memory, and ArrayBuffer statistics.
  • Introduce the native WorkerMemoryUsageTaker plus the per-isolate templates/bindings to expose this data.
  • Add a regression test (test/parallel/test-worker-get-memory-usage.js) and document the API in doc/api/worker_threads.md.

Testing

  • python3 tools/test.py parallel/test-worker-get-memory-usage
  • ninja -C out/Debug node

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Nov 18, 2025
@codecov
Copy link

codecov bot commented Nov 18, 2025

Codecov Report

❌ Patch coverage is 77.01149% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.54%. Comparing base (17fba60) to head (56ece73).
⚠️ Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
src/node_worker.cc 76.71% 7 Missing and 10 partials ⚠️
lib/internal/worker.js 78.57% 3 Missing ⚠️
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     
Files with missing lines Coverage Δ
src/node_worker.h 90.90% <ø> (ø)
lib/internal/worker.js 96.16% <78.57%> (-0.36%) ⬇️
src/node_worker.cc 81.18% <76.71%> (-0.42%) ⬇️

... and 34 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Number::New(isolate, heap_stats.external_memory()),
Number::New(isolate, allocator == nullptr
? 0
: allocator->total_mem_usage()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no guarantee that allocator is still alive at this point

<!-- YAML
added:
changes:
- version: v25.2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pr-url: https://github.com/nodejs/node/pull/REPLACEME
pr-url: https://github.com/nodejs/node/pull/60778

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants