Search Unity

Metal GPU Memory Crash due to leaking ScratchBuffers when resuming application

Discussion in 'iOS and tvOS' started by Lucas_TDF, Jun 8, 2022.

  1. Lucas_TDF

    Lucas_TDF

    Joined:
    Apr 4, 2022
    Posts:
    9
    Hi!

    We just upgraded our project from Unity 2019 to Unity 2021.3 and we are seeing a very strange crash in our iOS build.
    We have a restart routine that we call when resuming the application after it's been in background for more than 30s. This function is triggered in the OnApplicationFocus callback. The problem we have is that during restart the memory of the application will go from 800mb to > 2g of memory in a few seconds and will be shutdown by the os.

    From the logs we can see that everything in our restart routine works as expected (unloading scenes, cleaning assets and services) but after doing some Metal profiling via XCode the problem seems to be that the GPU is creating 2 new 4mb ScratchBuffers every frame leading to a memory warning from the os and then a shutdown. If we profile a regular frame from the game we can see that we have only 12 ScratchBuffers (named ScratchBuffer_00 to _06 with two of each)

    Some more details:
    • This was not happening in previous build and we're only seeing it since upgrading Unity
    • When upgrading Unity we've moved the restart logic from OnApplicationPause (deprecated) to OnApplicationFocus with Custom Background Behaviors enabled in the Project settings.
    • Restarting the application while its running works perfectly fine
    • Resuming the application if it's been in the background for less than 30s (ie our restart routine does not start) works perfectly fine.
    • If the phone is locked while the app was active (in foreground) then the restart works perfectly fine
    • If the app is put to background before locking the phone (just by swipping up and going back to the phone desktop) then the app will crash with the described issue when resuming.
    Does anyone has any idea which subsystem creates Buffers named "ScrachBuffers_xx"? Or some details on how Unity (or iOS) deals with the application when its running in the background?

    Is restarting the game a good practice when getting back focus? also is the game supposed to pause itself or will that be done automatically by the OS?

    This is a tough bug and any help will be greatly appreciated! Thanks! :)
     
  2. chaostheorygames

    chaostheorygames

    Joined:
    Jul 10, 2014
    Posts:
    12
    Hello, we also are experiencing a similar bug. Not sure what it is either. We've solved it before and have just recieved it again
    upload_2022-7-19_14-29-37.png
     
  3. Lucas_TDF

    Lucas_TDF

    Joined:
    Apr 4, 2022
    Posts:
    9
    Hi @chaostheorygames, "glad" to see we're not the only ones dealing with that. So far we havent figured out what is causing that our current process is to basically disable everything we can and re-enable it one by one see what could be causing this.
    Is that also only happening after a wake up from background or are you seeing it in other situations?

    Thanks
     
  4. chaostheorygames

    chaostheorygames

    Joined:
    Jul 10, 2014
    Posts:
    12
    Apparently the IOS app ram limit decreases when the app is defocused. So maybe that's related to your problem but seems like a stretch.

    Unfortunately this is the only solution we can think of as well.

    Our crash occurs when the game loads into a scene and spawns a bunch of assets. No drama on Android.
     
  5. chaostheorygames

    chaostheorygames

    Joined:
    Jul 10, 2014
    Posts:
    12
    @Lucas_TDF We've solved this by upgrading our Unity version from 2021.2.11 to 2021.2.17. Hope that helps.

    Try checking the version notes for your unity version, and see if you can find a known issue for the bug, then keep going forward in versions until its resolved. Update to that version and hopefully that fixes it.
     
  6. james510762

    james510762

    Joined:
    Sep 27, 2022
    Posts:
    1
    Any update for this? I'm using Unity 2020.3.35. After opening third-party native pages (eg. Helpshift) on our iOS device, the memory raise drastically and shutdown in few seconds. I checked out the profiling tool and it's the same problem with Metal and URP.
    I wonder whether upgrading Unity to 2021.3 will help.
     
  7. Lucas_TDF

    Lucas_TDF

    Joined:
    Apr 4, 2022
    Posts:
    9
    Hi James, I'm sorry but I dont have any update on this. Our solution was to rollback to 2020.3 where the problem does not happen on our end.
     
    james510762 likes this.