Open History to see the last commit
1import { useEffect } from "react";
2import { AppState } from "react-native";
3import { scheduleOnUI } from "react-native-worklets";
4
5function collectUiRuntimeGarbage() {
6 "worklet";
7 const gc = (globalThis as { gc?: () => void }).gc;
8 if (typeof gc === "function") gc();
9}
10
11/**
12 * React Native only collects the main JS runtime on an iOS memory warning. The
13 * worklets UI runtime is a separate Hermes heap that holds native shadow nodes and
14 * serializables alive until its own GC runs, so collect it on the same warning.
15 */
16export function useUiRuntimeMemoryWarningGc() {
17 useEffect(() => {
18 const subscription = AppState.addEventListener("memoryWarning", () => {
19 scheduleOnUI(collectUiRuntimeGarbage);
20 });
21 return () => subscription.remove();
22 }, []);
23}