Skip to content

fix(🧠): free Dawn's unused memory on memory pressure and in the background - #653

Closed
andreialecu wants to merge 3 commits into
wcandillon:mainfrom
andreialecu:fix/reduce-memory-usage
Closed

andreialecu wants to merge 3 commits into
wcandillon:mainfrom
andreialecu:fix/reduce-memory-usage

Conversation

@andreialecu

@andreialecu andreialecu commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

This is a precaution. I haven't run into a problem caused by it, but giving GPU memory back when the system asks for it is good practice, and other GPU embedders do it. Nothing in this library calls dawn::native::ReduceMemoryUsage, while:

  • Chrome calls it from its GPU cleanup path, and runs it again while it reports pending work (graphite_cache_controller.cc).
  • Flutter frees its GPU resources on iOS memory warnings and when the app goes to the background (observer, handlers), and its Metal layer drops its spare drawable textures in the background (FlutterMetalLayer.mm).
  • Dawn's header says what the call is for: it frees unused GPU memory such as staging buffers and cached resources, and should be run again after a short delay while it returns true (DawnNative.h).

The point is to make the app less likely to be jetsammed. Jetsam is the iOS mechanism that kills apps when the device runs low on memory. It starts with background apps and goes by how much memory each one uses, so an app that gives memory back when it gets a memory warning or goes to the background is less likely to be picked. Android's low memory killer works in a similar way, which is why the change covers both platforms.

Agent-written content

GPUDevice::reduceMemoryUsage() calls dawn::native::ReduceMemoryUsage on every live device. It goes over the registry the same way flushPipelineCaches() does (that loop is now liveDevices()), with the same threading rules:

  • a device that synchronizes itself is trimmed on the calling thread, since Dawn's exported wrapper takes the device lock;
  • any other device gets a job on its runtime's thread, which holds the wrapper weakly;
  • imported devices are skipped, because their owner looks after them.

When it runs:

  • iOS: on UIApplicationDidReceiveMemoryWarningNotification and UIApplicationDidEnterBackgroundNotification. macOS is unchanged.
  • Android: on ComponentCallbacks2.onTrimMemory at TRIM_MEMORY_UI_HIDDEN or above, and on onLowMemory. It does not run on onHostPause, which also fires for a permission dialog or a multi-window focus change, after which the app comes straight back.

If Dawn reports memory still waiting on GPU work, the call runs again 250 ms later, as Chrome does, for at most 4 passes. On Vulkan Dawn always reports pending work, because the submit it makes to free memory leaves work of its own. On the emulator the first pass freed everything there was to free and the next seven freed nothing, which is why there is a cap.

At the pinned Dawn, a trim frees the last staging ring buffer (kRingBufferSize, 4 MiB), the scratch and temporary uniform buffers, and trims the bind group layout and error scope caches. On Vulkan it also frees recycled allocator memory and clears the framebuffer cache. Metal has no ReduceMemoryUsageImpl, so on iOS the gain is those 4 MiB plus the caches.

Measured on an Android 15 emulator (Cube example, app sent to the background), with ComputeEstimatedMemoryUsageInfo and GetAllocatorMemoryInfo:

before after
Buffers 4,195,828 B 1,524 B
Vulkan allocator (allocated) 22,036,480 B 22,036,480 B
Textures 9,029,334 B 9,029,334 B

The app rendered normally when brought back.

Tests

MemoryTrim.spec.ts (Android only) uploads with writeBuffer and reads the data back in a loop, while it sends the example app to the background with HOME and brings it back. It checks that every round reads back what it wrote and that the device still works afterwards. It can't use am send-trim-memory, because Android refuses a background trim level for a process in the foreground.

On an Android 15 emulator the spec passes, a temporary log showed the trim running during the uploads, and the spec fails with MapAsyncStatus::Aborted when the trim is replaced with Destroy(). iOS has no command-line way to send a memory warning, so the spec skips there. The iOS changes were compiled (the react-native-webgpu pod, for the simulator) but not run.

…round

Call dawn::native::ReduceMemoryUsage on every live device when iOS sends a
memory warning or the app enters the background, and when Android trims
memory with its UI hidden or memory low. Repeat after a short delay while
Dawn reports memory waiting on GPU work in flight, as Chrome does.

On Metal this frees Dawn's last staging ring buffer (4 MiB) plus scratch,
temporary uniform and cache memory. On Vulkan it also frees recycled device
memory and the framebuffer cache.
On Vulkan, Dawn's ReduceMemoryUsage keeps reporting pending work: it submits
to free what waits, and that submit leaves work of its own. Measured on an
Android 15 emulator (Cube example, app sent to the background): the first
pass freed Dawn's 4 MiB staging ring buffer; the next seven freed nothing
and still reported pending.
Sends the example app to the background (TRIM_MEMORY_UI_HIDDEN) and back
while it uploads through writeBuffer and reads back, then checks every
round read back what it wrote and the device still works afterwards.
Android only: iOS has no command-line memory warning. am send-trim-memory
cannot be used, as Android refuses a background level for a foreground
process.

Checked on an Android 15 emulator: the trim runs during the uploads, and
the spec fails (MapAsyncStatus::Aborted) when the trim destroys the device.
@wcandillon

Copy link
Copy Markdown
Owner

this is a duplicate of #649 but I will check if there are some ideas there

@andreialecu

Copy link
Copy Markdown
Contributor Author

Oh wow, 3 hours too late, haha. Good, feel free to close this one.

wcandillon added a commit that referenced this pull request Nov 25, 2026
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 <andreialecu@users.noreply.github.com>
@wcandillon

Copy link
Copy Markdown
Owner

closing it but I had a look at it, thank you

@wcandillon wcandillon closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants