FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

v8: add setHeapProfileNearHeapLimit by IlyasShabi · Pull Request #64676 · nodejs/node · GitHub

/ node Public

v8: add setHeapProfileNearHeapLimit - #64676

Open
IlyasShabi wants to merge 1 commit into
nodejs:mainfrom
IlyasShabi:ishabi/near-heap-limit-profile
Open

v8: add setHeapProfileNearHeapLimit#64676
IlyasShabi wants to merge 1 commit into
nodejs:mainfrom
IlyasShabi:ishabi/near-heap-limit-profile

Conversation

IlyasShabi commented Jul 22, 2026
edited
Loading

Copy link
Copy Markdown
Member

This PR adds the v8.setHeapProfileNearHeapLimit(limit) API, which writes the active sampling heap profile to disk when V8 approaches the heap limit. It complements v8.setHeapSnapshotNearHeapLimit() use cases that need a sampled allocation profile rather than a full heap snapshot.

Heap profiling must first be enabled using v8.startHeapProfile() or the --heap-prof CLI option.

v8.setHeapProfileNearHeapLimit() and v8.setHeapSnapshotNearHeapLimit() can be active on the same isolate.

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 Jul 22, 2026
IlyasShabi marked this pull request as ready for review July 22, 2026 14:03
Comment thread src/env.cc Outdated
Comment thread doc/api/v8.md

joyeecheung commented Jul 22, 2026
edited
Loading

Copy link
Copy Markdown
Member

I struggle to see how this API can ever stabilize - what one can do when the heap is near the heap limit is very limited, and in particular if you do too much in the callback to incur another allocation, the program would be in a very unstable state. Our current callbacks only do very limited things within the C++, allowing users to do arbitrary things in JS sounds like an API that invites bugs...I think a safer option would be to only write a profile when it reaches the limit, though even then I am not sure how well the profiler works when the heap is about to reach the limit...

Copy link
Copy Markdown
Member

I'm also wary on adding a JS callback to be invoked at new heap limit. This callback also takes a heap profile parameter which is taken on a big heap, sounds like exacerbating the problem it wants to fix.

Comment thread doc/api/v8.md Outdated

v8.startHeapProfile();
v8.setHeapProfileNearHeapLimit((profile) => {
console.log(JSON.parse(profile));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This example asks users to exacerbating the heap usages when the heap is near limit...

Copy link
Copy Markdown
Member Author

This API is not intended to make JS safe near OOM. It closes a blind spot in the sampling heap profiler: today, if a process starts startHeapProfile() and reaches a fatal OOM, the accumulated sampling data is lost because stopHeapProfile() is never called so the API provides a best-effort chance to retrieve that data before the process terminates.
The samples are collected continuously, so the near-limit event does not take a full heap snapshot or walk every heap object. However profiler->GetAllocationProfile() still materializes the sampled call tree and may allocate, so capture itself is also best-effort.

Copy link
Copy Markdown
Member

Why would the application want to save the profile in JS memory when it's likely not relevent for the application itself that may already crash soon? I think a file-backed API would achieve the same thing without making the users waste the remaining memory with (arguably very noisy) diagnostic data.

IlyasShabi requested a review from Qard July 23, 2026 09:46

Copy link
Copy Markdown
Member Author

@joyeecheung Yes I agree with your proposal! I changed the approach to a file-backed one. please feel free to review it

IlyasShabi requested a review from legendecas July 27, 2026 09:42

codecov Bot commented Jul 27, 2026
edited
Loading

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 49.24242% with 67 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.12%. Comparing base (984e46f) to head (63db010).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
src/env.cc 20.27% 56 Missing and 3 partials ⚠️
src/node_v8.cc 72.22% 0 Missing and 5 partials ⚠️
src/env-inl.h 92.00% 0 Missing and 2 partials ⚠️
src/node_worker.cc 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #64676      +/-   ##
==========================================
- Coverage   90.12%   90.12%   -0.01%     
==========================================
  Files         751      751              
  Lines      253629   253767     +138     
  Branches    47789    47808      +19     
==========================================
+ Hits       228575   228697     +122     
- Misses      16305    16312       +7     
- Partials     8749     8758       +9     
Files with missing lines Coverage Δ
lib/v8.js 98.79% <100.00%> (+0.02%) ⬆️
src/env.h 98.21% <ø> (ø)
src/node_profiling.cc 81.08% <ø> (-0.26%) ⬇️
src/node_worker.cc 82.07% <66.66%> (+0.25%) ⬆️
src/env-inl.h 94.85% <92.00%> (-0.18%) ⬇️
src/node_v8.cc 87.36% <72.22%> (-0.51%) ⬇️
src/env.cc 82.24% <20.27%> (-3.11%) ⬇️

... 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.

legendecas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If I understand the motivation correctly, the intent is to write out the samples from a long-running heap profiler before the process dies of OOM, rather than losing them.

limit makes sense for setHeapSnapshotNearHeapLimit(). A snapshot is a point-in-time object graph, so each capture is a genuinely new state, N of them are diff-able, and Chrome DevTools supports comparing them out of the box.

The sampling profiler is different. It maintains a running live-set keyed by allocation site, so for diagnostic purposes, profile N+1 answers everything profile N does, which makes every file but the last redundant.

I wonder if the API can be simplified as a single final dump for the heap profiler?

Comment thread lib/v8.js Outdated

Copy link
Copy Markdown
Member Author

@legendecas The sampler's live set isn't monotonic, so profile N+1 isn't a superset of profile N (with the default flags kSamplingNoFlags) a major GC always runs between two near-heap-limit callbacks, since those are the last-resort GC paths. So each profile drops whatever died since the previous one, which is often the spike that triggered it.

limit here is a budget on how many times we do expensive allocating work on a near-OOM path.

legendecas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks for clarifying. Would you mind applying the same permission check added on #64808? Thanks

IlyasShabi requested a review from legendecas August 25, 2026 09:02
legendecas added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 25, 2026
github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 25, 2026

Copy link
Copy Markdown
Collaborator

Copy link
Copy Markdown
Member

Would you mind rebasing the commits and squash them? CI is not really good at handling merge commits.

Signed-off-by: ishabi <ilyasshabi94@gmail.com>
IlyasShabi force-pushed the ishabi/near-heap-limit-profile branch from 02481e8 to 63db010 Compare August 25, 2026 19:06
IlyasShabi added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 25, 2026
github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 25, 2026

Copy link
Copy Markdown
Collaborator

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

5 participants


Back | FazBrowse Home | New Git URL