react / react/react-native

High memory usage for `Text` component on iOS

未关闭
#54,847 1 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Needs: Triage :mag: Platform: iOS
主要语言
C++
星标
127k
派生
25.3k
平均合并
1 天 23 小时
30 天内合并 PR
4

描述

Description

When working on memory optimisation of a complex RN app, I found that Text components on iOS can consume relatively large amounts of memory. When I've checked that with Xcode Instruments, it seems that each RCTParagraphTextView is accompanied by a backing CALayer which consumes memory proportional to its rendered size, more precisely width x height x 4 bytes (RGBA channels).

While individual Text instances are small, complex UI, esp. using stacked screens & virtualized lists can add up to quite large number (tens of MB), especially if the app uses large font sizes in such lists.

For example, each of the following Text components consumes about 112-140 KB of memory, due to the backing raster.

Image

This can be observed using Allocations Xcode Instrument by marking memory before and after adding text views.

Image

I've created a small app for easily measuring various components' memory usage: https://github.com/mdjastrzebski/rn-memory-footprint

Note:

  • on Android, this issue does not seem to happen, as all Text have similar size, regardless of used font size
  • on iOS, when using native UILabel, these are much smaller, and do not have CALayers attached
Steps to reproduce
  1. Download the reproducer app
  2. yarn install & yarn ios:release OR yarn android:release
  3. Run the app on a simulator or a device
  4. Attach Allocation Xcode Instrument, mark generation A
  5. Pick "Text (Large Font)" from the component dropdown
  6. Press "Create Views"
  7. Wait a couple of seconds, mark generation B in instruments
  8. Compare memory usage. You should see a lot of RCTParagraphTextView (CALayer) entries, each taking 120-140 KBs.
React Native Version

0.82.1

Affected Platforms

Runtime - iOS

Output of npx @react-native-community/cli info
System:
  OS: macOS 26.1
  CPU: (11) arm64 Apple M3 Pro
  Memory: 102.75 MB / 36.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 22.17.1
    path: /Users/mdj/.nvm/versions/node/v22.17.1/bin/node
  Yarn:
    version: 4.12.0
    path: /Users/mdj/.nvm/versions/node/v22.17.1/bin/yarn
  npm:
    version: 10.9.2
    path: /Users/mdj/.nvm/versions/node/v22.17.1/bin/npm
  Watchman:
    version: 2025.07.28.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.16.2
    path: /Users/mdj/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 25.0
      - iOS 26.0
      - macOS 26.0
      - tvOS 26.0
      - visionOS 26.0
      - watchOS 26.0
  Android SDK:
    API Levels:
      - "35"
      - "36"
    Build Tools:
      - 35.0.0
      - 36.0.0
    System Images:
      - android-35 | Google Play ARM 64 v8a
      - android-36 | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2025.2 AI-252.25557.131.2521.14432022
  Xcode:
    version: 26.0.1/17A400
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.16
    path: /usr/bin/javac
  Ruby:
    version: 3.4.5
    path: /Users/mdj/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 20.0.0
    wanted: 20.0.0
  react:
    installed: 19.1.1
    wanted: 19.1.1
  react-native:
    installed: 0.82.1
    wanted: 0.82.1
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true
Stacktrace or Logs
n/a
MANDATORY Reproducer

https://github.com/mdjastrzebski/rn-memory-footprint

Screenshots and Videos

Added above

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

使用“Text (Large Font)”组件和 Xcode Allocations Instrument,通过链接的 rn-memory-footprint 应用复现该问题。首先跟踪 RCTParagraphTextView 及其底层 CALayer,然后将观察到的 iOS 行为与原生 UILabel 和 Android 进行比较;在不改变 Text 行为的情况下解决每个视图过高的内存使用量,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
ios, react-native
领域
mobile, performance
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。