Skip to content

Commit 6e13d8f

Browse files
fix(mobile): collect UI runtime garbage on iOS memory warnings
React Native only collects the main Hermes runtime when iOS posts a memory warning. The worklets UI runtime is a separate heap that keeps native shadow nodes and synchronizables alive until its own GC runs. Schedule a UI-runtime GC on the same AppState memoryWarning event. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 4ff5c69 commit 6e13d8f

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎apps/mobile/src/App.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { appAtomRegistry } from "./state/atom-registry";
2222
import { OverlayPortalHost } from "./components/OverlayPortal";
2323
import { shouldHandleAppLink } from "./lib/appLinking";
2424
import { useMobileNavigationTheme } from "./lib/useMobileNavigationTheme";
25+
import { useUiRuntimeMemoryWarningGc } from "./lib/useUiRuntimeMemoryWarningGc";
2526
import { SubscriptionUsageCoordinator } from "./widgets/SubscriptionUsageCoordinator";
2627
import { VoiceInputProvider } from "./features/voice-input/VoiceInputProvider";
2728
import { GlobalVoiceInputControl } from "./features/voice-input/GlobalVoiceInputControl";
@@ -56,6 +57,8 @@ function SplashScreenCoordinator() {
5657
}
5758

5859
export default function App() {
60+
useUiRuntimeMemoryWarningGc();
61+
5962
return (
6063
<RegistryContext.Provider value={appAtomRegistry}>
6164
<CloudAuthProvider>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useEffect } from "react";
2+
import { AppState } from "react-native";
3+
import { scheduleOnUI } from "react-native-worklets";
4+
5+
function collectUiRuntimeGarbage() {
6+
"worklet";
7+
// Hermes exposes `gc` on the worklets UI runtime; skip quietly on runtimes that do not.
8+
const gc = (globalThis as { gc?: () => void }).gc;
9+
if (typeof gc === "function") gc();
10+
}
11+
12+
/**
13+
* React Native only collects the main JS runtime on an iOS memory warning. The
14+
* worklets UI runtime is a separate Hermes heap that holds native shadow nodes and
15+
* serializables alive until its own GC runs, so collect it on the same warning.
16+
*/
17+
export function useUiRuntimeMemoryWarningGc() {
18+
useEffect(() => {
19+
const subscription = AppState.addEventListener("memoryWarning", () => {
20+
scheduleOnUI(collectUiRuntimeGarbage);
21+
});
22+
return () => subscription.remove();
23+
}, []);
24+
}

0 commit comments

Comments
 (0)