-
-
Notifications
You must be signed in to change notification settings - Fork 36.2k
v8: add setHeapProfileNearHeapLimit #64676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
IlyasShabi
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
IlyasShabi:ishabi/near-heap-limit-profile
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+401
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1853,6 +1853,56 @@ const profile = handle.stop(); | |
| console.log(profile); | ||
| ``` | ||
|
|
||
| ## `v8.setHeapProfileNearHeapLimit(onProfile, options)` | ||
|
|
||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
|
|
||
| > Stability: 1 - Experimental | ||
|
|
||
| * `onProfile` {Function} Callback invoked with the captured heap profile. | ||
| * `profile` {string} A JSON heap profile string with the same shape as | ||
| [`syncHeapProfileHandle.stop()`][]. | ||
| * `options` {Object} | ||
| * `maxExtensions` {integer} Maximum number of times V8 is allowed to extend | ||
| the heap limit for this registration before letting OOM proceed. After the | ||
| budget is exhausted the callback uninstalls itself after the pending | ||
| profile is delivered. **Default:** `1`. | ||
| * `extensionSize` {integer} Number of bytes to add to the heap limit for | ||
| each extension. **Default:** `10 * 1024 * 1024` (10 MiB). | ||
|
|
||
| Installs a callback that captures the active sampling heap profile when V8 is | ||
| near the heap limit. The callback extends the heap limit up to `maxExtensions` | ||
| times by `extensionSize` bytes so the profile can be delivered to JavaScript. | ||
|
|
||
| [`v8.startHeapProfile()`][] must be called before this API can capture a | ||
| profile. If no sampling heap profile is active when V8 reaches the heap limit, | ||
| the callback is uninstalled and V8 continues its normal out-of-memory behavior. | ||
|
|
||
| The callback is best-effort. If the process cannot continue after the heap | ||
| extension, `onProfile` might not run. | ||
|
|
||
| Calling `setHeapProfileNearHeapLimit()` more than once is a no-op; the first | ||
| registration wins and subsequent calls are silently ignored. | ||
|
|
||
| This API is independent of [`v8.setHeapSnapshotNearHeapLimit()`][] and | ||
| `--heapsnapshot-near-heap-limit`. Both can be active on the same isolate; on a | ||
| near-heap-limit event Node.js invokes every registered handler and uses the | ||
| largest returned heap limit. Each handler maintains its own budget. | ||
|
|
||
| ```cjs | ||
| const v8 = require('node:v8'); | ||
|
|
||
| v8.startHeapProfile(); | ||
| v8.setHeapProfileNearHeapLimit((profile) => { | ||
| console.log(JSON.parse(profile)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example asks users to exacerbating the heap usages when the heap is near limit... |
||
| }, { | ||
| maxExtensions: 2, | ||
| extensionSize: 64 * 1024 * 1024, | ||
| }); | ||
| ``` | ||
|
|
||
| [CppHeap]: https://v8docs.nodesource.com/node-22.4/d9/dc4/classv8_1_1_cpp_heap.html | ||
| [HTML structured clone algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm | ||
| [Hook Callbacks]: #hook-callbacks | ||
|
|
@@ -1870,6 +1920,10 @@ console.log(profile); | |
| [`GetHeapSpaceStatistics`]: https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33378f8f57e1d13a4 | ||
| [`NODE_V8_COVERAGE`]: cli.md#node_v8_coveragedir | ||
| [`Serializer`]: #class-v8serializer | ||
| [`syncHeapProfileHandle.stop()`]: #syncheapprofilehandlestop | ||
| [`v8.setHeapProfileNearHeapLimit()`]: #v8setheapprofilenearheaplimitonprofile-options | ||
| [`v8.setHeapSnapshotNearHeapLimit()`]: #v8setheapsnapshotnearheaplimitlimit | ||
| [`v8.startHeapProfile()`]: #v8startheapprofileoptions | ||
| [`after` callback]: #afterpromise | ||
| [`async_hooks`]: async_hooks.md | ||
| [`before` callback]: #beforepromise | ||
|
|
||
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.