Skip to content

macOS: a hidden window keeps its full-size drawables, textures and buffers #161

Open

Description

@godismyjudge95

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:

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

  1. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    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 current dev stands (from reading the code; I haven't measured on macOS):

    • Already freed on hide: Host::hide_window (crates/rune/src/window/host/lifecycle.rs) now sends RestEvent::Unseen. That runs give_back(true), which frees the renderer's spare and scratch targets, waits for the GPU, trims staging and upload chunks (trim_uploads), and runs ImageAtlas::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.
    • Still kept:
      • ImageAtlas::rest (crates/rune-image/src/pixels.rs) keeps slots that were shown in the last frame while their Pixels is alive. So the screen-size image a window showed just before hiding keeps its page and texture.
      • Nothing on hide touches the CAMetalLayer. setDrawableSize is only called from configure (crates/rune-gpu/src/metal/surface.rs), so the drawables stay at full size and no smaller frame is presented.
      • Windows have no App::release_gpu_when_hidden. Only release_gpu_when_closed exists.

    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.

  2. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    I haven't made a change for this yet. Current dev already 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 runs give_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 its Pixels.

    Still kept: the CAMetalLayer drawables. Nothing touches the layer on hide; setDrawableSize is only called from configure (crates/rune-gpu/src/metal/surface.rs). It isn't a small change:

    • An ordered-out window's surface reports Occluded, so a 1x1 frame can't be presented after the hide.
    • Presenting one before the hide may flash on screen.
    • Your test showed that only setting drawableSize doesn't free the main window's surface.

    Picking between these needs footprint runs on macOS.

    If you can, a fresh footprint -v table on current dev (main window hidden, and after region captures) would help. It would show how much is still left beyond the drawables.

  3. godismyjudge95 commented on Nov 27, 2026

    @godismyjudge95
    Author

    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 dev dacf5d89 with no patches (native Metal backend), against the same app on GPUI. footprint -v 18 s after captures, with the main window, the region overlay window and the HUD all hidden:

    GPUI Rune dev
    footprint 71 MB 141 MB
    IOSurface 34 MB 67 MB
    Owned physical footprint (unmapped) (graphics), resident 0 13 MB
    untagged (VM_ALLOCATE) 1.7 MB 31 MB
    IOAccelerator (graphics) 14 MB 3.9 MB

    The IOSurface difference is the drawables. footprint names each CAMetalLayer Display Drawable:

    hidden window GPUI Rune dev
    main window one 2200x1520, one 1x1 two 2200x1520
    region overlay one 2880x1800 two 2880x1800
    HUD one 552x432 two 552x432

    So 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 maximumDrawableCount drawables 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 Pixels that 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 drawableSize while hidden, and set the size again before the next show.


    Posted via Claude

  4. Hona commented on Nov 28, 2026

    @Hona
    Member

    Thanks @godismyjudge95 for the fresh footprint -v breakdown on dacf5d89 — 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) onto dev to address the hidden-window GPU retention:

    • Shrink idle CAMetalLayer drawables to 1×1 on hide (macOS): Added an explicit HiddenDrawable state machine and platform::macos::set_drawable_size via gpu::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, unless release_gpu_when_hidden), Rune sets CAMetalLayer.drawableSize to 1×1 so 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.
    • Drop full-window retained damage texture on unseen rest (all native platforms): Renderer::give_back(true, ...) now releases DamageState::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.
    • Trim deferred overlay upload chunks: When OverlayCore::retire.poll_due finishes retiring in-flight work while rested, it now calls renderer.trim_uploads().

    Verification:

    • Verified locally (Windows, test-gpu-counters): cargo check -p rune --lib, HiddenDrawable state-machine and shrink-policy tests, give_back_unseen_drops_retained_damage_texture_and_wakes_cleanly (confirming HAL texture count returns to baseline on give_back(true) and pixel-exact wake), overlay::rest_tests, retire_state_machine, and window::tests all pass.
    • Not run on real macOS hardware: The detached CAMetalLayer test (metal_layer_shrinks_drawable_size_while_hidden_and_restores_before_draw) and live macOS footprint -v measurements were not run locally on a Mac.

    Leaving #161 open for now — when you have a chance, could you rerun footprint -v on dev (3705e457d or 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-side Pixels retention and atlas page buffers remain tracked in #2675 and #163.)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions