Skip to content

feat(🧹): add purgeGpuResources(), and purge when the system is short of memory - #649

Merged
wcandillon merged 7 commits into
mainfrom
feat/purge-gpu-resources
Nov 25, 2026
Merged

wcandillon merged 7 commits into
mainfrom
feat/purge-gpu-resources

Conversation

@wcandillon

Copy link
Copy Markdown
Owner

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 Android onLowMemory and onTrimMemory from TRIM_MEMORY_BACKGROUND (Android 14+ never calls onLowMemory). A no-op on the Web.

  • Imported devices are skipped: their owner purges them.
  • Devices with implicitDeviceSynchronization: false are purged on their runtime's thread.
  • Each pass ticks first: ReduceMemoryUsage learns 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 (onTrimMemory rows compare with the app in the background):

after destroy() 3 s later
512² × 200, control 1400 1400
512² × 200, purge 1400 604
1024² × 60, control 1330 1330
1024² × 60, purge 1330-1333 613-617
background, control 1400 1358
background, onTrimMemory 1400 541

Two rounds each with logging in; the 512² purge, control and trim arms again on the final code, same result.

  • E2E, final code: iOS simulator 49 suites, 248 tests pass. Samsung 48 suites, 247 tests pass, without ABuffer.spec: it crashes the app in Qualcomm's shader compiler on this phone, also in the build from Nov 22 without this change.
  • iOS memory warning (simulator, lldb): 3/3 reach ReduceMemoryUsage.

Not verified: memory on iOS, frame-time cost, Mali, device loss during a purge, onLowMemory on older Android, how much an unsynchronized device gets back.

…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.
wcandillon and others added 2 commits November 25, 2026 09:19
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.
@wcandillon

Copy link
Copy Markdown
Owner Author

Agent-written content: memory measurements on 33be520

Two commits since the description was written:

  • 0f87b59 moves the purge out of GPUDevice into GpuPurge (no change in behavior).
  • 33be520 purges from TRIM_MEMORY_UI_HIDDEN on Android instead of TRIM_MEMORY_BACKGROUND, for the reason measured below. The trigger line of the description is out of date on that point.

Samsung S25 Edge (Android 17, Adreno 830), debug build

Example app on the e2e screen (no render loop). rgba8unorm textures are created, filled, then destroyed. Total PSS from dumpsys meminfo, in MB, sampled from the host. Baseline is 510 to 520.

Foreground

after destroy() 3 s later 10 s later
512² × 200, filled by a render pass, control 910 910 910
512² × 200, filled by a render pass, purgeGpuResources() 912 516
1024² × 60, filled by a render pass, control 753 753 753
1024² × 60, filled by a render pass, purgeGpuResources() 753 516
512² × 200, filled by writeTexture, control 1326 1326 1326
512² × 200, filled by writeTexture, purgeGpuResources() 1327 520

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)

after destroy() 3 s after home later
0f87b59 (TRIM_MEMORY_BACKGROUND) 881 871 869 at 60 s, 794 at 120 s, Graphics unchanged at 441
0f87b59, another app opened so that the example becomes a cached process 897 858 Graphics unchanged at 427 for 60 s
0f87b59, am send-trim-memory ... BACKGROUND 897 443
33be520 (TRIM_MEMORY_UI_HIDDEN) 915 475 475 at 10 s

A temporary log in onTrimMemory showed which levels the system sends: level 20 (TRIM_MEMORY_UI_HIDDEN) once each time the app leaves the foreground, and nothing else. Level 40 never came, with the process in state LAST, then CACHED (adj 900), where a separate check showed it frozen. So with the previous threshold the purge did not run on its own on this phone.

iOS simulator (iOS 26.5, debug build with ASan)

Measured on 0f87b59 (33be520 only changes the Android threshold). Process footprint from footprint, in MB. Texture memory does not show in the footprint of a simulator process (the render pass arms stay flat at 656 to 660), so only upload staging memory can be read there:

allocated after destroy() 3 s later 10 s later
512² × 200, filled by writeTexture, control 887 887 887 887
512² × 200, filled by writeTexture, purgeGpuResources() 888 888 663

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, _performMemoryWarning): GpuPurge::onMemoryWarning() is reached, then one pass (GPUDevice::reduceMemoryUsage(), reduceMemoryUsageOnRuntimeThreads() and dawn::native::ReduceMemoryUsage hit once each).

E2E on 33be520

  • Samsung: 50 suites, 257 tests pass, without ABuffer.spec as before.
  • iOS simulator: 51 suites, 258 tests pass.

Not measured yet: texture memory on a physical iPhone, Mali, frame-time cost.

@wcandillon

Copy link
Copy Markdown
Owner Author

Agent-written content: iPhone 14 Pro (iOS 27.0.1), debug build of 33be520

The same arms on a physical iPhone. Physical footprint of the app in MB, recorded from the host while the arms ran (xctrace, Activity Monitor template, one sample a second). Baseline is 430 to 440.

allocated after destroy() 10 s later 3 s after purgeGpuResources()
512² × 200, filled by a render pass, control 761 451 429 429
512² × 200, filled by a render pass, purge 652 430 430
1024² × 60, filled by a render pass, control 687 451 439 432
1024² × 60, filled by a render pass, purge 682 453 440
512² × 200, filled by writeTexture, control 839 635 635 435
512² × 200, filled by writeTexture, purge 840 651 459

In the control arms the purge is the cleanup at the end of the arm.

On Metal, destroy() gives the texture memory back at once, with or without a purge. What an idle app keeps is the staging memory of its writeTexture uploads, about 200 MB here, and that is what the purge returns. This matches the simulator reading above and closes the "texture memory on a physical iPhone" item.

wcandillon added a commit to wcandillon/react-native-skia that referenced this pull request Nov 25, 2026
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.
@wcandillon
wcandillon merged commit d2930e0 into main Nov 25, 2026
9 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.25.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant