Skip to content

fix: forward Android memory pressure to the UI worklet runtime - #11758

Draft
AuYuHui wants to merge 1 commit into
software-mansion:mainfrom
AuYuHui:codex/android-worklets-memory-pressure
Draft

AuYuHui wants to merge 1 commit into
software-mansion:mainfrom
AuYuHui:codex/android-worklets-memory-pressure

Conversation

@AuYuHui

@AuYuHui AuYuHui commented Dec 4, 2026 •

Copy link
Copy Markdown

Note

This pull request was authored by AI on behalf of @AuYuHui.

Summary

Refs #11501. Android forwards memory pressure to the React Native runtime, but objects held by the separate UI Worklet Runtime can stay alive until that runtime collects.

Once the UI runtime has started, the Android Worklets module registers for the same trim levels that trigger a React Native collection. It posts to the JS queue, then schedules native JSI collection on the UI runtime. This keeps collection outside the Android callback. Invalidation unregisters the listener and guards queued callbacks.

Test plan

Changelog and C++ formatting checks passed. The changed translation unit passed an Android arm64 syntax check, and the changed Kotlin file compiled against Android SDK 37 and React Native 0.88.1. A full Android build and device validation are pending.

Use the reproducer from #11501 with a release build of the PR. Run the animated and plain arms with scripts/laps.sh animated 20 and scripts/laps.sh plain 20 on Android 13. Compare Native Heap Alloc before and after the script sends RUNNING_CRITICAL, before pressing its manual GC buttons. The expected result is that the animated arm releases inactive native objects after trim and the manual UI GC button has less additional memory to release.

On current Android versions, background the app and also exercise TRIM_MEMORY_BACKGROUND. Check that a trim after a React Native reload reaches the new instance and that neither reload nor teardown crashes. Android 14 and later no longer naturally deliver RUNNING_CRITICAL; the shell command in the repro triggers it explicitly.

Changelog

  • Added a Worklets changelog fragment.

Android 内存压力回调触发 RN Runtime 的垃圾回收,UI Worklet Runtime 仍可能持有已卸载的原生对象。模块启动后注册回调,经 JS 队列调度 UI Runtime 的回收,并在模块失效时注销。
@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Android memory-pressure callbacks now request garbage collection on the UI Worklet Runtime. The callbacks register after startup and unregister during invalidation. Collection requests pass through the JS queue and run only while the module remains valid. The native method schedules collection with the reason "TRIM_MEMORY".

Possibly related issues

  • Issue 11501: The PR forwards Android trim events to UI-runtime garbage collection, addressing the reported failure to release UI Worklet Runtime memory on those events.

Suggested labels: lib: Worklets

Change: Bug fix

Merge Risk: Minimal · up to 1dda7

Android memory-pressure events will now also trigger garbage collection on the UI Worklet Runtime. No concrete merge-blocking risk was found. Device validation is still pending.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: Android memory-pressure events are forwarded to the UI worklet runtime. The changeset adds trim-level callbacks in WorkletsModule.kt and a native collectUIRuntimeGarba…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description states that Android memory-pressure events are forwarded to the UI Worklet Runtime for garbage collection. It names the module registration after start, the JS queue dispatch, and the …
Full details: Title check

Explanation

The title names the main change: Android memory-pressure events are forwarded to the UI worklet runtime. The changeset adds trim-level callbacks in WorkletsModule.kt and a native collectUIRuntimeGarbageCpp method that schedules UI runtime GC, which matches the title.

Full details: Description check

Explanation

The description states that Android memory-pressure events are forwarded to the UI Worklet Runtime for garbage collection. It names the module registration after start, the JS queue dispatch, and the invalidation cleanup. It also gives concrete test steps and a changelog checkbox. These details match the changed WorkletsModule.kt, WorkletsModule.cpp/.h, and changelog fragment.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4fe96c69-4912-4a10-bcb7-7ab6708415e6
Commits

Reviewing files that changed from the base of the PR and between 9abf7cd and 1dda7c3.

Files selected for processing: 4

  • packages/react-native-worklets/android/src/main/cpp/worklets/android/WorkletsModule.cpp
  • packages/react-native-worklets/android/src/main/cpp/worklets/android/WorkletsModule.h
  • packages/react-native-worklets/android/src/main/java/com/swmansion/worklets/WorkletsModule.kt
  • packages/react-native-worklets/changelog/android-ui-runtime-memory-pressure.fix.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant