-
Notifications
You must be signed in to change notification settings - Fork 948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak on MacOS | M1 OSX #1783
Comments
Similar report in #1537 |
On my Intel MacBook Pro it works as expected, I think its a M1 SoC related issue. |
I don't have an M1. We need help investigating this. |
I've been debugging a similar (same?) issue the last couple days. It has something to do with In Edit: forgot to mention you also need to put the window in fullscreen mode. In windowed mode, the framerate gets clamped. |
With the high framerate modification above, if I remove the |
Also, note: I first noticed this bug with |
Also maybe noteworthy: with the |
It's not just when the window' fullscreen, but when it's completely obscured. Adding a |
what device are you running on? the cube example won't leak memory on my m1 air even when I uncap framerate in |
M1 Air for me as well. |
oh interesting. i don't seem to incur any 'leaking' by obscuring one window using another (if that's what you mean) but the process does demand a lot of virtual memory e.g. 5 gigabytes after a few seconds of being on another workspace. i'm now a little hesitant to call my issue a memory leak since the memory seems to be cleaning itself up while the process is running and returning to normal (wgpu master branch). |
@jbatez could you attach the modified files you used to make the cube example leak? if so i can verify whether mine exhibits similar behaviour. could you also tell me what version of osx you're on? i'm on 11.5.1 |
My repro is as follows,
I also tried on a intel mbp, where I did see a tiny increase in allocations that plateaued, and which drop off to earlier levels after bringing the window back. The wgpu examples' framework does some frame limiting which I think is hiding this for them. Making the framework unconditionally call the The allocations are clearly just calls for I would draw the conclusion that when the window is occluded, In this shadow example trace you can kind of see it But I'm not really sure who's bug this is. The behavior seems unexpected, so I would think it is actually an metal/os bug (it should still be adhering to Also, googling this I keep ending up in gfx-rs/gfx#2460 :) |
For me it grows until my system locks up and I need to hold the power button to restart. Activity Monitor says ~60 GB when that happens. I only have 8 GB of physical.
Sure, 1 minute.
11.4 |
See my fork: Just run |
Also, just noticed the window obscuring behavior (e.g., just use the Activity Monitor window to completely obscure the cube window) just uncaps the framerate, but doesn't cause the memory usage to run out of control. |
Looks like it might have something to do with my screen setup. I'm hooked up to a monitor through a Thunderbolt dock. When I unplug my MacBook and use its builtin display, the problem goes away. |
thanks for attaching it! this cube example isn't really giving me any issues. for me, the memory usage maybe grows a little when obscuring the window with another but only briefly (no big deal). nothing bad happens on my machine when i maximise the window (regardless of whether i stretch to maximise or maximise onto another workspace). |
What's your display setup? |
could you try running the tutorial code from the original post with your display unplugged? i believe that's the core issue. to reproduce, run the code for tutorial 4 and switch onto a workspace not containing the window. warning: the memory usage spikes in the order of gigabytes after just a few seconds for me. not using any external displays. |
Not a problem until I go fullscreen or completely obscure the window. And in this case, obscuring the window does cause the memory usage to shoot up. Same case with or without an external display. If you don't have any more tests in-mind for me, I'd like to try updating to macOS 11.5.2 and seeing how that behaves. |
I tried testing so that winit doesn't emit |
fwiw, the Xcode Metal Game template uses In WGPU, I see a call to I'm aware the |
This happens to me on an Intel MacBook on 10.14.6 whenever my game is backgrounded, so it's not just an M1 bug. In my testing it got significantly worse with WGPU 0.10 (700MB/s leak) vs 0.9 (around 10MB/s) which is how I noticed it. |
This happens on my M1 Air too. It only occurs when the window is in a workspace I am not currently using. If I run it without a different workspace the ram usage is significantly lower. Detecting if the window is not occluded before rendering, like hinted above seems. to fix the problem. |
I think I've figured out what's going on in #1936. Basically in some newer Big Sur versions and especially on the M1 SoC, Metal's AnimationCoreKit will shut off some housekeeping tasks like reclaiming certain resources and buffers when the window isn't in focus. The Apple recommended way to solve this seems to be to stop rendering when |
Since we don't control the window visibility, it lies on the user shoulders, if I understand correctly. Is there anything that can be done on our side? |
@kvark We need to expose some The way I figured this out was basically spending the last 6 or so hours inspecting disassembled Apple code blobs and tracing in a debugger so it's shady at best. |
I further confirmed this by stopping rendering when winit issued a |
I wonder if it's related to https://www.macworld.com/article/549755/m1-macbook-app-memory-leaks-macos.html in any way |
I don't think it is, I recently got an M1 MacBook Air (8/256) and haven't experienced any memory issues apart from WGPU (what took 40M on my linux machine takes 300M on me Mac). Though I could be wrong here as the article only talks about the new MacBook Pros. I don't know in what capacity it would help, but I would like to say that I am willing to test code on my M1 Mac because you said you didn't have one. |
I've been playing around with learn-wgpu/tutorial4-buffer.rs a bit, and I have noticed that it uses much less memory when using PresentMode::Immediate and for some reason, with RUST_LOG=info enabled. |
My take on the leak, especially considering #1783 (comment) is that the issue is very timing-dependent. (I don't have M1 hardware, just chiming in with outside observations.) More logging will affect timing, as will changing the presentation mode. |
It's somewhat hard to follow the thread. I see a lot of people investigated this and concluded, apparently, that
|
@jbatez the reason this example uses semaphores is not because it wouldn't block otherwise. |
We're having the same issue as what @Matt-hde links from. |
@kvark Could you clarify what you're referring to here? |
macOS has an application called "Feedback Assistant" which one uses to report issues back to Apple. |
I'm having this same problem on Windows with Vulkan. I get a |
That might be a separate issue. You shouldn't be requesting new frames when the window has a size of 0. |
@cwfitzgerald That solved my problem. Thank you |
any updates? the issue still persists with v0.16 and the latest winit version (i tried with |
I ran a small program with Google's Dawn. They have the same issue... Edit: after banging my head over this issue, I think it's safe to say it's Apple's problem. |
This doesn't happen when we run on moltenvk, so whatever they're doing we should be doing. |
Take a look here for how to search for leaks on mac https://github.com/gfx-rs/wgpu/wiki/Debugging-wgpu-Applications#mac-leaks |
I tested on my intel mac, instrumenting with this: diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs
index 0ddf96ed4..715467fad 100644
--- a/wgpu-hal/src/metal/mod.rs
+++ b/wgpu-hal/src/metal/mod.rs
@@ -35,6 +35,7 @@ use std::{
use arrayvec::ArrayVec;
use bitflags::bitflags;
use metal::foreign_types::ForeignTypeRef as _;
+use objc::{msg_send,sel, sel_impl};
use parking_lot::{Mutex, RwLock};
#[derive(Clone, Debug)]
@@ -352,6 +353,14 @@ pub struct SurfaceTexture {
present_with_transaction: bool,
}
+impl Drop for SurfaceTexture {
+ fn drop(&mut self) {
+ let tret :usize = unsafe { msg_send![self.texture.raw, retainCount] };
+ let dret :usize = unsafe { msg_send![self.drawable, retainCount] };
+ eprintln!("drop SurfaceTexture tex.ret={tret} drawable.ret={dret}");
+ }
+}
+
impl std::borrow::Borrow<Texture> for SurfaceTexture {
fn borrow(&self) -> &Texture {
&self.texture And I see the texture On a quick glance, the release also helped with the memory leak. This didn't fix the odd hang that happens when cmd-tabbing away though, hmm. Too late to investigate further. |
At long last, this tests as fixed by #4781 |
Description
Some users of my tutorial have been experiencing memory issues on my buffer tutorial. It seems to only be an issue on the new M1 chips.
Repro steps
You'll need a mac with and M1 chip, then run the tutorial code here https://github.com/sotrh/learn-wgpu/tree/master/code/beginner/tutorial4-buffer. If you have the repo already downloaded you can just run
cargo run --bin tutorial4-buffer
.Expected vs observed behavior
The expected behaviour is no memory leaks on M1
Extra materials
I don't have a Mac, so I can't provide hardware specifics other that it's occurring on M1, but the I'll link the issue from my repo here. sotrh/learn-wgpu#207
Platform
OSX with M1 chip, wgpu 0.9
The text was updated successfully, but these errors were encountered: