Unmounted Canvas keeps its last image resident until the UI runtime GCs (recorder and picture are never disposed)
Description
When a Canvas that draws an SkImage unmounts, the image's pixels stay resident even after the app calls image.dispose(). For a full-screen makeImageFromView snapshot that is ~16 MB per image on an iPad and ~12.6 MB on an iPhone 17 Pro.
The memory is released eventually, but only after the UI worklet runtime runs a GC and Skia later drains its deferred-deletion queue on that thread. On a screen with no other Skia or Reanimated activity that can take a long time. In our app it lasted for as long as the user stayed on the screen.
This is not #3869, which was an unbounded leak fixed in 2.7.0. The retention here is bounded, and the images are freed once something else animates. It is related to #4077, since both come from NativeReanimatedContainer creating a new JsiRecorder on every redraw and leaving the old one to the GC.
Cause (src/sksg/Container.native.ts, NativeReanimatedContainer):
redraw()creates a newReanimatedRecorderevery time and only drops the previousJsiRecorder. That recorder's commands hold nativesk_spreferences to everything they draw, for example theSkImagebehind an<ImageShader>or<Image>.unmount()stops the mapper (#3932) but leaves two objects alive: the lastJsiRecorder, andthis.picture, aJsiSkPicturewhoseSkPicturealso references the image.- Both objects are captured by worklets, so they live on the UI runtime. Hermes only sees the JS wrappers there, not the native memory behind them, so nothing prompts it to collect them.
image.dispose()only drops the reference held by the image's own JS wrapper.
Evidence (Release build, iOS 27 simulator, iPad Pro 11-inch (M5)). The app takes full-screen makeImageFromView snapshots on each theme change, shows them in a Canvas for an animated transition, then unmounts the overlay and disposes the images. After 4 theme changes and 15 s idle on the same screen:
vmmapshowsCG Raster Dataholding 49 MB, which is three 16,154,624-byte regions.malloc_historyattributes each one toRNSkApplePlatformContext::takeScreenshotFromViewTag→-[ViewScreenshotService screenshotOfViewWithTag:]→CGBitmapContextCreateImage→CGDataProviderCreateWithCopyOfData.heapshows 8 liveRNSkia::JsiRecorder, 9RNSkia::JsiSkPictureand 4RNSkia::JsiSkImage, even though no overlayCanvasis mounted.- Navigating to a screen that runs other Skia animations releases the regions. That fits a GC plus deferred deletion rather than a true leak.
We have a fix that disposes the retired recorder on redraw, and the recorder and picture on unmount. With it, the 49 MB drops to 1.6–2.4 MB and the footprint at that point goes from 240–242 MB to 202–205 MB (3 runs each). The PR follows.
React Native Skia Version
2.12.0 (latest; main has the same code in Container.native.ts)
React Native Version
0.86.3 (Expo SDK 57, Hermes, react-native-reanimated 4.6.0, react-native-worklets 0.12.2)
Using New Architecture
- Enabled
Steps to Reproduce
- Take a full-screen snapshot with
makeImageFromView. - Draw it in a
Canvas, for example<Image>or<ImageShader>, optionally with animated props. - Unmount the
Canvasand calldispose()on the image. - Stay on a screen without other Skia or Reanimated activity and watch
footprint <pid>or theCG Raster Datainvmmap <pid>(iOS). The pixel buffer stays resident until something else triggers a GC on the UI runtime.
We found this in our app, Muqun (dev.osuki.muqun), in a theme-change transition, measured on Release builds. The code path is shared JS, so the retention mechanism is the same on Android. On Android, however, our app draws only a small image through this path, so there we could not measure a difference.
Snack, Code Example, Screenshot, or Link to Repository
This is a sketch of the pattern, reduced from our app. The measurements above come from the app, not from running this snippet on its own.
const ref = useRef<View>(null);
const [shot, setShot] = useState<SkImage | null>(null);
const take = async () => setShot(await makeImageFromView(ref));
const drop = () => {
const old = shot;
setShot(null); // unmounts the Canvas
requestAnimationFrame(() => old?.dispose()); // pixels stay resident
};
return (
<View ref={ref} collapsable={false} style={{ flex: 1 }}>
{/* ...content... */}
{shot && (
<Canvas style={StyleSheet.absoluteFill}>
<Image image={shot} x={0} y={0} width={w} height={h} fit="cover" />
</Canvas>
)}
</View>
);Source: Shopify/react-native-skia