Skip to content

macOS: release_gpu_when_closed leaks one window-size surface on every hide and show #162

Not planned

Description

@godismyjudge95

What happens

With App::release_gpu_when_closed, each hide destroys the window and its GPU device, and the next show makes new ones. On macOS the process footprint grows by one window-size BGRA surface on every cycle and never comes back. The growth is in "Owned physical footprint (unmapped)": memory charged to the process but mapped by nobody in it.

Where

At dev de2b16de, window/lifecycle.rs#L291-L297 drops win and gpu on hide (app.rs#L301-L304 documents the option). We did not find what keeps the surface.

Measured

macOS 26.6 VM (paravirtual Metal GPU), screen 2880x1696, Rune 7ead7d63 (both files above are the same on dev except three lines that add maximize). A full-screen, borderless, hidden-by-default window with keep_running().release_gpu_when_closed(); each cycle shows it, draws a screen-size image for about 0.5 s, and hides it. footprint -v and heap 3 s after each cycle:

cycle footprint owned unmapped IOSurface objects WinitWindowDelegate objects
1 137 MB 19 MB 4 2
2 156 MB 37 MB 4 2
5 212 MB 93 MB 4 2
10 306 MB 187 MB 4 2

The growth is 18.7 MB per cycle, the size of one 2880x1696 BGRA surface (19.5 MB). The object counts stay the same, so the process holds no reference to it. Inference, not traced: the window server or the layer's render context keeps the last presented surface of the destroyed window, charged to our process.

Without the option the same 10 cycles stay at 197 MB, but the hidden window then keeps about 81 MB (see the related issue).

Repro

  1. A window app with keep_running().release_gpu_when_closed() whose view shows a full-screen image.
  2. Repeat 10 times: AppHandle::open, wait for a frame, AppHandle::hide, wait 3 s.
  3. Read footprint -v <pid> after each cycle; the "Owned physical footprint (unmapped)" line grows by one surface per cycle.

The numbers are from our app; we have not run this minimal repro.

Expected

A hide and show cycle with release_gpu_when_closed returns the process to the same footprint, within a few MB, after any number of cycles.

Done when: 10 cycles on macOS end within a few MB of the first, and a test or probe that counts the cycles checks it.

Related: #80, and #161, the hidden-window memory that this option would solve if it did not leak.


Posted via Claude

Activity

  1. Hona commented on Nov 23, 2026

    @Hona
    Member

    On current dev, Host::release drops every reference Rune holds to the window, view, CAMetalLayer, drawables, and GPU (crates/rune/src/window/host/lifecycle.rs, crates/rune/src/window/surface/win.rs, crates/rune-gpu/src/metal/surface.rs). A 10-cycle windowless Metal probe on Apple silicon at 2880x1696 freed the layers each cycle with flat footprint.

    No PR has changed this path for a real NSWindow, so we do not know whether Owned physical footprint (unmapped) still grows on current dev, on bare-metal Apple silicon, or with RUNE_GPU_BACKEND=wgpu. Because IOSurface and WinitWindowDelegate counts stayed flat, the surface appears to be held by WindowServer or the VM's paravirtual GPU.

    If this still reproduces on current dev, please reopen with a repro:

    1. Build a minimal repro with App::keep_running and App::release_gpu_when_closed showing a full-screen borderless image (--release or --profile perf).
    2. Run 10 cycles: AppHandle::open, wait ~1 s, AppHandle::hide, wait 3 s.
    3. Share footprint -v <pid> and heap <pid> | grep -E 'IOSurface|WinitWindowDelegate|CAMetalLayer|RawWindowMetalLayer' after cycles 1 and 10, plus a run with RUNE_GPU_BACKEND=wgpu (and bare-metal Apple silicon if available).

    Thanks for the detailed report and measurements.

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