Repository navigation
Description
What happens
On macOS, AppHandle::hide (and a close with keep_running) orders the window out but keeps everything it drew with: the CAMetalLayer's drawables at full size, the renderer's textures and atlas pages, and shared Metal buffers (32 MB after an overlay showed a screen-size image; we think upload buffers, but did not trace them). A hidden window draws no more frames, so nothing frees them until it shows again. A menu-bar app that keeps a main window and display-size overlay windows hidden most of the time pays for all of them all the time.
Where
At dev de2b16de:
- window/lifecycle.rs#L303-L310: the hide branch for a window without
release_gpu_when_closedkeepswin,gpuand the renderer. - gpu/metal/surface.rs#L112-L113: the layer keeps up to three drawables at the configured size.
- window/win.rs#L48-L50: the size is set again only when the window's size changes, so an app cannot shrink the layer for Rune.
- pixels.rs#L300-L316: image pages are freed only at the end of a drawn frame.
release_gpu_when_closedfrees all of this but leaks one drawable per cycle on macOS (macOS: release_gpu_when_closed leaks one window-size surface on every hide and show #162).
Measured
macOS 26.6 VM (paravirtual Metal GPU), screen 2880x1696, Rune 7ead7d63 (the files above are the same on dev). footprint -v 2.5 s after each step, one probe run:
| state | IOSurface | IOAccelerator (graphics) | private textures | total |
|---|---|---|---|---|
| main window shown, idle | 26 MB (2 drawables 2200x1456) | 20 MB | 32 MB | 135 MB |
| main window hidden | 26 MB | 20 MB | 32 MB | 135 MB |
| after region captures, all hidden | 64 MB (+2 drawables 2880x1696) | 34 MB | 36 MB | 197 MB |
With release_gpu_when_closed on the overlay window, the same state is 27, 2.2 and 24 MB (before its leak), so about 81 MB belongs to the hidden overlay window alone.
The same app on our previous toolkit set a hidden window's drawables to 1x1 and kept one full-size surface per window. Its hidden total is 71 to 75 MB. Against it, with 5 alternating runs per side: idle in the tray 51.8 MB vs 133.5 MB, after 8 captures in the tray 69.5 MB vs 221.4 MB.
We tried the same from the app: set drawableSize to 1x1 on the hidden window's CAMetalLayer and back before the next show. For overlay windows this gives back one drawable (in the tray after captures 221.3 to 202.8 MB, reopened 254.8 to 236.2 MB, 5 runs each, p = 0.008). For the main window it gave nothing (idle in the tray 133.5 to 133.4 MB): the freed IOSurface moved to "Owned physical footprint (unmapped)", so something outside the process still holds it. Inference: the window server keeps the last presented surface until the layer presents a smaller one, and a hidden Rune window never presents again.
Ask
When a window hides (and release_gpu_when_closed is not set), give back what only its frames need: present or attach a 1x1 drawable so the full-size surfaces go, and drop its image pages and the buffers of its last frames. The next show draws again at full size. An opt-in such as App::release_gpu_when_hidden, like the overlay option, is fine too.
Done when: on macOS a window that showed a screen-size image and was then hidden holds no full-size IOSurface and no image texture, footprint returns to within a few MB of a window that never showed, and showing it again draws correctly.
Related: #142 (the same for Wayland overlays), #80 (memory policy), #162 (the release option leaks on macOS).
Posted via Claude
Activity
opencodejr commentedon Nov 25, 2026
Thanks for the detailed measurements. I'm treating this as a feature request: when a window hides, give back what only its frames need, without releasing the whole GPU.
The code has moved a lot since
de2b16de, so here is where currentdevstands (from reading the code; I haven't measured on macOS):Host::hide_window(crates/rune/src/window/host/lifecycle.rs) now sendsRestEvent::Unseen. That runsgive_back(true), which frees the renderer's spare and scratch targets, waits for the GPU, trims staging and upload chunks (trim_uploads), and runsImageAtlas::rest(fix(overlay): reclaim spare GPU pools and runtime caches while resting #1933). This may cover part of the 32 MB of shared buffers you saw.ImageAtlas::rest(crates/rune-image/src/pixels.rs) keeps slots that were shown in the last frame while theirPixelsis alive. So the screen-size image a window showed just before hiding keeps its page and texture.CAMetalLayer.setDrawableSizeis only called fromconfigure(crates/rune-gpu/src/metal/surface.rs), so the drawables stay at full size and no smaller frame is presented.App::release_gpu_when_hidden. Onlyrelease_gpu_when_closedexists.That leaves the drawable shrink (presenting a 1x1 frame so the window server drops the full-size surface, as your main-window result suggests) and dropping the last frame's image pages on hide. Your done criteria (no full-size IOSurface or image texture while hidden, footprint within a few MB of a window that never showed, correct redraw on show) are clear targets.
opencodejr commentedon Nov 25, 2026
I haven't made a change for this yet. Current
devalready covers part of the ask, and the remaining part needs testing on a Mac, which I can't do here.Already freed when a window hides.
Host::hide_window(crates/rune/src/window/host/lifecycle.rs) now runsgive_back(true). That drops every GPU image page texture (Renderer::clear_images), the layer and spare targets, the backdrop copies and the blur pyramid. It then waits for the GPU and trims the staging and upload chunks (trim_uploads). So the image textures and the last frames' buffers in your table should already go on hide. Only the CPU copy of an image the last frame showed stays, while the app still holds itsPixels.Still kept: the
CAMetalLayerdrawables. Nothing touches the layer on hide;setDrawableSizeis only called fromconfigure(crates/rune-gpu/src/metal/surface.rs). It isn't a small change:Occluded, so a 1x1 frame can't be presented after the hide.drawableSizedoesn't free the main window's surface.Picking between these needs
footprintruns on macOS.If you can, a fresh
footprint -vtable on currentdev(main window hidden, and after region captures) would help. It would show how much is still left beyond the drawables.godismyjudge95 commentedon Nov 27, 2026
You asked for numbers from a Mac. Here they are, on current
dev.macOS 26.6 VM (paravirtual Metal GPU), screen 2880x1800, our screenshot app on Rune
devdacf5d89with no patches (native Metal backend), against the same app on GPUI.footprint -v18 s after captures, with the main window, the region overlay window and the HUD all hidden:devThe IOSurface difference is the drawables.
footprintnames eachCAMetalLayer Display Drawable:devSo the image textures and staging now go at hide, as you wrote (IOAccelerator is lower than on GPUI). What stays is a second full-size drawable in each hidden window: 34 MB in this state. The layer keeps up to
maximumDrawableCountdrawables at the configured size (rune-gpu/src/metal/surface.rs#L118-L119). GPUI keeps one full-size drawable in each hidden window.The 13 MB "owned unmapped" entry is 817 resident pages (12.8 MB, the size of one 2200x1520 BGRA surface). GPUI has entries of the same size with 0 resident pages. We did not trace its owner.
The other 29 MB of the gap is CPU memory: 13.9 MB is the
Pixelsthat the last view of a hidden window still holds (#2675), and 10.5 MB is atlas page buffers (#163).What would close the drawable part: when a window hides, keep at most one full-size drawable and let the others go, for example with a 1x1
drawableSizewhile hidden, and set the size again before the next show.Posted via Claude
fix(macos,render): shrink hidden Metal drawables to 1x1 and drop reta…
Hona commentedon Nov 28, 2026
Thanks @godismyjudge95 for the fresh
footprint -vbreakdown ondacf5d89— isolating the exact drawable count per hidden window and the 12.8 MB owned-unmapped entry made the GPU-side gaps clear.Merged #2687 (
3705e457d) ontodevto address the hidden-window GPU retention:CAMetalLayerdrawables to1×1on hide (macOS): Added an explicitHiddenDrawablestate machine andplatform::macos::set_drawable_sizeviagpu::with_layer(covering both Rune's native Metal backend and wgpu's Metal backend). When a non-launcher window hides (Host::hide_window) or an overlay finishes hiding after any exit fade (OverlayCore::carry_out, unlessrelease_gpu_when_hidden), Rune setsCAMetalLayer.drawableSizeto1×1so Core Animation releases unused full-size drawables from its pool while leaving any displayed surface intact, and restores(config.width, config.height)before the next show or drawable acquire.Renderer::give_back(true, ...)now releasesDamageState::retained(a full-window GPU texture held by opaque windows that used partial damage, e.g. ~12.8 MB at 2200×1520), recreating it cleanly on wake with a whole-frame catch-up (KeptDraw::Whole). Visible idle rest (give_back(false, ...)) keeps it warm.OverlayCore::retire.poll_duefinishes retiring in-flight work while rested, it now callsrenderer.trim_uploads().Verification:
test-gpu-counters):cargo check -p rune --lib,HiddenDrawablestate-machine and shrink-policy tests,give_back_unseen_drops_retained_damage_texture_and_wakes_cleanly(confirming HAL texture count returns to baseline ongive_back(true)and pixel-exact wake),overlay::rest_tests,retire_state_machine, andwindow::testsall pass.CAMetalLayertest (metal_layer_shrinks_drawable_size_while_hidden_and_restores_before_draw) and live macOSfootprint -vmeasurements were not run locally on a Mac.Leaving #161 open for now — when you have a chance, could you rerun
footprint -vondev(3705e457dor newer) to verify whether the hidden IOSurface drawables and the 13 MB unmapped entry drop as expected and that re-showing draws cleanly? (The CPU-sidePixelsretention and atlas page buffers remain tracked in #2675 and #163.)