Repository navigation
fix(🧠): free Dawn's unused memory on memory pressure and in the background - #653
Closed
andreialecu wants to merge 3 commits into
Closed
andreialecu wants to merge 3 commits into
andreialecu wants to merge 3 commits into
Conversation
…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.
Owner
|
this is a duplicate of #649 but I will check if there are some ideas there |
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>
Owner
|
closing it but I had a look at it, thank you |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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()callsdawn::native::ReduceMemoryUsageon every live device. It goes over the registry the same wayflushPipelineCaches()does (that loop is nowliveDevices()), with the same threading rules:When it runs:
UIApplicationDidReceiveMemoryWarningNotificationandUIApplicationDidEnterBackgroundNotification. macOS is unchanged.ComponentCallbacks2.onTrimMemoryatTRIM_MEMORY_UI_HIDDENor above, and ononLowMemory. It does not run ononHostPause, 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 noReduceMemoryUsageImpl, 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
ComputeEstimatedMemoryUsageInfoandGetAllocatorMemoryInfo:The app rendered normally when brought back.
Tests
MemoryTrim.spec.ts(Android only) uploads withwriteBufferand 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 useam 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::Abortedwhen the trim is replaced withDestroy(). iOS has no command-line way to send a memory warning, so the spec skips there. The iOS changes were compiled (thereact-native-webgpupod, for the simulator) but not run.