Repository navigation
Description
With AppleGfxApi::Metal, process memory grows steadily while the app is
running and redrawing. With AppleGfxApi::OpenGl and the same program, it
stays flat.
Minimal repro
[dependencies]
macroquad = { version = "=0.4.16", default-features = false } # miniquad 0.4.11use macroquad::prelude::*;
fn conf() -> Conf {
let metal = std::env::var("GFX").as_deref() == Ok("metal");
Conf {
window_title: "mq-metal-leak".to_owned(),
platform: miniquad::conf::Platform {
apple_gfx_api: if metal {
miniquad::conf::AppleGfxApi::Metal
} else {
miniquad::conf::AppleGfxApi::OpenGl
},
..Default::default()
},
..Default::default()
}
}
#[macroquad::main(conf)]
async fn main() {
loop {
clear_background(BLACK);
draw_rectangle(10.0, 10.0, 100.0, 100.0, GREEN);
next_frame().await
}
}Run GFX=metal cargo run --release and sample RSS every ~10 s
(ps -o rss= -p <pid>), then compare with plain cargo run --release (OpenGL).
Measurements
macOS 12.7.6, x86_64, Intel Iris Pro (Metal GPU family macOS 1), release builds.
Minimal repro above. With Metal, memory grows the whole time and
doesn't level off over 3 minutes. The GPU regions stay constant; the growth
is on the malloc heap:
| Backend | RSS, sampled every 30 s for 180 s | footprint: IOAccelerator (graphics) |
|---|---|---|
| Metal | 34 → 37 → 40 → 43 → 45 → 48 → 51 MB | constant: 14 MB in 141 regions |
| OpenGL (separate ~70 s run, 10 s samples) | 35 → 36 → 37 → 38 → 38 → 39 → 39 MB | 8.7 MB in 17 regions |
In a separate 90 s Metal run, footprint showed phys_footprint going from
51 MB to 104 MB. Almost all of that is MALLOC_SMALL, which went from absent to
48 MB.
heap -s <pid> after 60 s of the Metal repro shows per-frame Metal objects
staying alive:
COUNT BYTES AVG CLASS_NAME TYPE BINARY
273 6150144 22528.0 MTLIGAccelCommandBuffer._pStateHeaps (malloc) C AppleIntelHD5000GraphicsMTLDriver
273 205296 752.0 MTLIGAccelCommandBuffer ObjC AppleIntelHD5000GraphicsMTLDriver
819 196560 240.0 MTLRenderPassDescriptorInternal ObjC Metal
819 157248 192.0 MTLRenderPassColorAttachmentDescriptorInternal ObjC Metal
819 157248 192.0 MTLRenderPassDepthAttachmentDescriptorInternal ObjC Metal
819 157248 192.0 MTLRenderPassStencilAttachmentDescriptorInternal ObjC Metal
819 131040 160.0 MTLIGAccelRenderCommandEncoder ObjC AppleIntelHD5000GraphicsMTLDriver
A heavier app (a terminal emulator redrawing full-screen text every
frame) leaks much faster. Over about 120 s, the GPU-side regions grow too:
| Backend | RSS | footprint: IOAccelerator (graphics) |
MALLOC_NANO |
|---|---|---|---|
| Metal | 77 → 217 MB, still rising (~1.2 MB/s) | 213 MB in 28,545 regions | 130 MB |
| OpenGL | 78 → 89 MB, then flat for 80 s | 8.7 MB in 16 regions | 2.6 MB |
Likely cause (from reading the code; we haven't tested a fix)
- The Metal backend gets a new command buffer and drawable every frame:
msg_send, and
msg_send!(self.view, currentDrawable)(metal.rs:1457). Both return
autoreleased objects. - On macOS, miniquad runs its own event loop, "Basically reimplementing
msg_send![ns_app, run] here" (src/native/macos.rs:1461), instead of
[NSApp run]. - The crate has no
@autoreleasepool/NSAutoreleasePool/objc_autoreleasePoolPush
anywhere, so nothing drains those objects. AppKit's own run loop would
drain a pool every turn.
Hundreds of command buffers, render command encoders and render pass
descriptors staying alive in heap (above) is what an undrained autorelease
pool looks like: these are the per-frame objects Metal hands back autoreleased.
Suggested fix
Wrap each turn of the macOS event loop (or each Metal frame) in an autorelease
pool, e.g. objc_autoreleasePoolPush() / objc_autoreleasePoolPop() around
the loop body. We haven't built or measured a patched version.
Environment
macOS 12.7.6 (x86_64), Xcode 14.2, rustc 1.99.1, macroquad 0.4.16 /
miniquad 0.4.11. The code paths are the same on master 39c928f.
Activity