Repository navigation
Report the native memory of created shadow nodes to the JS GC #59702
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
Merged
meta-codesync
merged 3 commits into
react:main
from
hryhoriiK97:hermes-shadow-node-external-memory
Nov 5, 2026
Merged
Changes from all commits
Commits
Show all changes
3 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
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
67 changes: 67 additions & 0 deletions
67
packages/react-native/src/private/__tests__/ShadowNodeExternalMemory-itest.js
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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @fantom_mode * | ||
| * @fantom_disable_coverage | ||
| * @flow strict-local | ||
| * @format | ||
| */ | ||
|
|
||
| import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; | ||
|
|
||
| import * as Fantom from '@react-native/fantom'; | ||
| import nullthrows from 'nullthrows'; | ||
| import * as React from 'react'; | ||
| import {View} from 'react-native'; | ||
|
|
||
| // Must match `kCreatedShadowNodeNativeMemory` in UIManagerBinding.cpp. | ||
| const CREATED_SHADOW_NODE_NATIVE_MEMORY = 2048; | ||
|
|
||
| function getExternalMemoryAfterGC(): number { | ||
| global.gc(); | ||
| const stats = nullthrows( | ||
| nullthrows(global.HermesInternal).getInstrumentedStats, | ||
| )(); | ||
| const externalBytes = stats.js_externalBytes; | ||
| if (typeof externalBytes !== 'number') { | ||
| throw new Error('Expected js_externalBytes to be a number'); | ||
| } | ||
| return externalBytes; | ||
| } | ||
|
|
||
| describe('shadow node external memory', () => { | ||
| it('reports the native memory of created shadow nodes to the GC until they are collected', () => { | ||
| const before = getExternalMemoryAfterGC(); | ||
|
|
||
| let root: ?Fantom.Root = Fantom.createRoot(); | ||
| Fantom.runTask(() => { | ||
| nullthrows(root).render( | ||
| <View> | ||
| {Array.from({length: 100}, (_, i) => ( | ||
| <View key={i} /> | ||
| ))} | ||
| </View>, | ||
| ); | ||
| }); | ||
|
|
||
| // The 100 children and their parent are alive, so their native memory | ||
| // stays reported after a GC. | ||
| expect(getExternalMemoryAfterGC() - before).toBeGreaterThanOrEqual( | ||
| 101 * CREATED_SHADOW_NODE_NATIVE_MEMORY, | ||
| ); | ||
|
|
||
| Fantom.runTask(() => { | ||
| nullthrows(root).destroy(); | ||
| }); | ||
| root = null; | ||
|
|
||
| // Once the tree is unmounted and collected, its memory isn't reported | ||
| // anymore. | ||
| expect(getExternalMemoryAfterGC() - before).toBeLessThan( | ||
| CREATED_SHADOW_NODE_NATIVE_MEMORY, | ||
| ); | ||
| }); | ||
| }); | ||
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.