Repository navigation
feat(🧹): add purgeGpuResources(), and purge when the system is short of memory - #649
Conversation
…of memory Dawn keeps the memory of destroyed textures and buffers for reuse, and in an idle app the deletions wait for a tick nothing asks for. purgeGpuResources() hands that memory back to the system from a background thread, for every device created here. It also runs on iOS memory warnings, and on Android onLowMemory and onTrimMemory from TRIM_MEMORY_BACKGROUND.
GPUDevice is the wrapper of one device, and held the purge thread, its state and its pass timing as statics. They move to a class of their own, GpuPurge, as in react-native-skia (RNSkGpuPurge): - GpuPurge::request() is purgeGpuResources() from JS. The platform modules report what the system said (onMemoryWarning, onLowMemory, onTrimMemory) and GpuPurge decides what calls for a purge, so the threshold is no longer in Java. - GPUDevice keeps one pass, without the loop counter: reduceMemoryUsage() for the devices that synchronize themselves and reduceMemoryUsageOnRuntimeThreads() for the others. - liveDevices() is the registry walk that flushPipelineCaches() and the passes share (from #653). - The pass count, the pass interval and the trim level are named in one place. No change in behavior. Co-authored-by: Andrei Alecu <[email protected]>
On a Galaxy S25 Edge (Android 17) the system only sends TRIM_MEMORY_UI_HIDDEN when the app leaves the foreground. TRIM_MEMORY_BACKGROUND never came, with the app cached and frozen for a minute, so the purge never ran on its own and about 440 MB of destroyed textures stayed held. From TRIM_MEMORY_UI_HIDDEN, going home gives them back within 3 s. Chrome, Flutter on iOS and Android's own renderer also free their GPU caches when the app is hidden.
Agent-written content: memory measurements on 33be520Two commits since the description was written:
Samsung S25 Edge (Android 17, Adreno 830), debug buildExample app on the e2e screen (no render loop). Foreground
A first round gave the same picture (909 to 509, 708 to 471, 1282 to 474). Each control arm was purged afterwards and came back to the baseline. Background, nothing called from JS (512² × 200 filled by a render pass, destroyed, then the home button)
A temporary log in iOS simulator (iOS 26.5, debug build with ASan)Measured on 0f87b59 (33be520 only changes the Android threshold). Process footprint from
Baseline is 660. On Metal an idle app keeps the staging memory of its uploads until something ticks the device, and the purge returns it. Memory warning (lldb, E2E on 33be520
Not measured yet: texture memory on a physical iPhone, Mali, frame-time cost. |
Agent-written content: iPhone 14 Pro (iOS 27.0.1), debug build of 33be520The same arms on a physical iPhone. Physical footprint of the app in MB, recorded from the host while the arms ran (
In the control arms the purge is the cleanup at the end of the arm. On Metal, |
On a Galaxy S25 Edge (Android 17) the system only sends TRIM_MEMORY_UI_HIDDEN when the app leaves the foreground. TRIM_MEMORY_BACKGROUND never came, with the app cached and frozen for a minute (measured in wcandillon/react-native-webgpu#649), so the purge never ran on its own. From TRIM_MEMORY_UI_HIDDEN, going home with 100 disposed 512x512 surfaces gives 260 MB back within 3 s. Chrome, Flutter on iOS and Android's own renderer also free their GPU caches when the app is hidden.
It is meant for one rare case, and the purge now runs on its own when the app is hidden or the system is short of memory. The function stays exported, and its doc comment says when to call it and when not to.
|
🎉 This PR is included in version 0.25.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Description
Sister of wcandillon/react-native-skia#4520. Destroying a texture or buffer frees it inside Dawn, which keeps its memory for reuse, and an idle app does not even get that far: the deletion waits for a tick that nothing asks for.
purgeGpuResources()hands that memory back to the system, for every device created through this package. It returns at once; a background thread does the work. It also runs on iOS memory warnings, and on AndroidonLowMemoryandonTrimMemoryfromTRIM_MEMORY_BACKGROUND(Android 14+ never callsonLowMemory). A no-op on the Web.implicitDeviceSynchronization: falseare purged on their runtime's thread.ReduceMemoryUsagelearns what completed, but leaves what waits for it to a tick. Without it, nothing was freed.Agent-written content
Debug builds, based on 4199a0a (before the three commits now on
main, which touch none of these files).Samsung S25 Edge, total PSS in MB, textures filled then destroyed (
onTrimMemoryrows compare with the app in the background):destroy()onTrimMemoryTwo rounds each with logging in; the 512² purge, control and trim arms again on the final code, same result.
ABuffer.spec: it crashes the app in Qualcomm's shader compiler on this phone, also in the build from Nov 22 without this change.ReduceMemoryUsage.Not verified: memory on iOS, frame-time cost, Mali, device loss during a purge,
onLowMemoryon older Android, how much an unsynchronized device gets back.