facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
118.45k stars 24.25k forks source link

Noticeable memory consumption difference between Hermes and JSC on iOS #37686

Closed michalchudziak closed 9 months ago

michalchudziak commented 1 year ago

Description

As delineated in the issue https://github.com/facebook/hermes/issues/878, there appears to be an elevated memory usage on iOS when utilizing the Hermes JavaScript engine compared to the JavaScriptCore (JSC) engine.

This trend is noticeable even with a newly created application, where Hermes exhibits a substantial difference compared to JSC. The discrepancy seems to be increased by the complexity of an application, with larger applications demonstrating a ~20-30 MB higher memory consumption with Hermes.

While the overall elevated memory usage might be inherent to the Hermes engine itself, there is a particular allocation, significantly tied to the React Native core, which correlates with the size of the JavaScript bundle. This allocation is approximately ~1.17 MiB for a sample application, but increases to more than 20 MiB for larger applications. Specifically, it is an NSData object that is allocated when the executeApplicationScript function is invoked.

Here's a stack trace of the allocation in question:

image

React Native Version

0.71.8

Output of npx react-native info

System: OS: macOS 13.0.1 CPU: (10) arm64 Apple M1 Max Memory: 224.94 MB / 64.00 GB Shell: 5.9 - /opt/homebrew/bin/zsh Binaries: Node: 16.19.0 - /usr/local/bin/node Yarn: 1.22.18 - /opt/homebrew/bin/yarn npm: 8.19.2 - /opt/homebrew/bin/npm Watchman: 2023.01.23.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.12.1 - /usr/local/bin/pod SDKs: iOS SDK: Platforms: DriverKit 22.4, iOS 16.4, macOS 13.3, tvOS 16.4, watchOS 9.4 Android SDK: API Levels: 23, 25, 26, 28, 29, 30, 31, 32, 33 Build Tools: 28.0.3, 29.0.2, 30.0.2, 30.0.3, 31.0.0, 32.0.0, 32.1.0, 33.0.0, 33.0.0 System Images: android-29 | Google Play ARM 64 v8a, android-30 | Intel x86 Atom_64, android-30 | Google Play ARM 64 v8a, android-31 | Intel x86 Atom_64, android-31 | Google APIs ARM 64 v8a, android-32 | Google APIs ARM 64 v8a, android-33 | Google APIs ARM 64 v8a Android NDK: Not Found IDEs: Android Studio: 2021.3 AI-213.7172.25.2113.9014738 Xcode: 14.3.1/14E300c - /usr/bin/xcodebuild Languages: Java: 11.0.17 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 18.2.0 => 18.2.0 react-native: 0.71.8 => 0.71.8 react-native-macos: Not Found npmGlobalPackages: react-native: Not Found

Steps to reproduce

  1. Create a new app
    • npx react-native init TestHermes
  2. Build and run the Release version on the device (in my case it's iPhone 7 14.4, but it's visible also on other iOS devices eg. iPhone 12 Pro 16.X)
  3. Open Product > Profile > Allocations in Xcode (14.3.1)
  4. Start profiling & observe all heap allocations
    • Sort the allocations and notice the largest malloc
  5. Stop the recording
  6. Go to Podfile and set :hermes_enabled => false flag.
  7. Reinstall pods, clean the build, re-build and run the Release version on the device
  8. Open Product > Profile > Allocations in Xcode
  9. Start profiling & observe all heap allocations
    • Sort the allocations and compare allocations structure
  10. Stop the recording

Snack, code example, screenshot, or link to a repository

With hermes:

image image

Without hermes:

image image
cortinico commented 1 year ago

As delineated in the issue https://github.com/facebook/hermes/issues/878

Moving back to RN as per last comment on the linked issue

tmikov commented 1 year ago

Is it possible that on iOS the bytecode bundle isn't being memory mapped and is instead read into a memory allocation?

github-actions[bot] commented 9 months ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 9 months ago

This issue was closed because it has been stalled for 7 days with no activity.

trungthanhnt commented 1 month ago

Is there any update or comparison in the latest version of react-native? We care about performance and memory consumption for TrustKeys Web3 SocialFi.