Search Unity

Question Tracking down "Unknown" memory allocation leak

Discussion in 'Editor & General Support' started by dyamanoha_, Feb 26, 2023.

  1. dyamanoha_

    dyamanoha_

    Joined:
    Mar 17, 2013
    Posts:
    87
    The image below pretty much says it. I've got a pretty bad memory leak in my game right now and I'm not sure what types of things are tracked under the 'Unknown' memory category. Any pointers would be helpful.

    This leak appears to happen in both the editor and standalone builds. I'm using 2022.2.8

    upload_2023-2-25_21-15-45.png
     
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
  3. dyamanoha_

    dyamanoha_

    Joined:
    Mar 17, 2013
    Posts:
    87
    idyakonov likes this.
  4. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    My bad. I thought, it was a screenshot from the memory profiler module, which has similar stats.

    Can't you just look at the details then? Or does it not show you because it is non-managed memory?
     
  5. dyamanoha_

    dyamanoha_

    Joined:
    Mar 17, 2013
    Posts:
    87
    Yeah there's nothing there because of the type of memory it is.

    Something I figured out today is that this isn't actually a leak. I wrote a very simple cross fade shader via a URP render feature (it's literally)

    Code (CSharp):
    1.             half4 frag(Varyings IN) : SV_Target
    2.             {
    3.                 return _TargetWeight * tex2D(_PrimaryTex, IN.texcoord)
    4.                     + (1. - _TargetWeight) * tex2D(_SecondaryTex, IN.texcoord);
    5.             }
    .. and what's happening is that when I turn off vsync and when this shader is running, the game's framerate becomes erratic like, between 2 FPS and 200 and watching the process in task manager I see the executable's memory footprint shoot up from 500 mb to upwards 6 gb, and then rapidly come back down afterward.

    The thing that has me flummoxed now is why this only seems to happen when the game is running at a high frame-rate and where the heck all this unmanaged memory is coming from? It's somewhat difficult to reproduce in-editor and debug builds but is pretty consistent in an optimized release one.
     
  6. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    It's certainly not the shader itself but hard to tell whether it is a CPU or GPU problem.
    I would profile the standalone build with NSight Systems and optionally with NSight graphics.
    Maybe somebody knows also a good memory profiler for native memory.
     
  7. dyamanoha_

    dyamanoha_

    Joined:
    Mar 17, 2013
    Posts:
    87
    Interesting, I hadn't heard of that tool. I'll give it a go tonight. I also need to carefully review how the render feature I wrote gets setup on a per-frame basis. It's pretty simple but I didn't put a ton of care into it.
     
  8. dyamanoha_

    dyamanoha_

    Joined:
    Mar 17, 2013
    Posts:
    87
    I'm just beginning to try and parse this data, but thought I'd share what the app looks like in one of the simplest scenes. pretty much static geometry, not scripts not really running any per. frame logic.

    upload_2023-2-27_20-18-34.png

    Why is every other frame long (~2ms / ~10ms)
     
  9. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    Good question, no idea. Hope you'll find something in there that will give you a clue.
     
  10. dyamanoha_

    dyamanoha_

    Joined:
    Mar 17, 2013
    Posts:
    87
    Things look more consistent with maxQueuedFrames set to 3 instead of 2; I'm guessing with 2 swapchain buffers the CPU is getting blocked by the GPU?
    upload_2023-2-27_20-38-19.png
     

    Attached Files:

    c0d3_m0nk3y likes this.
  11. dyamanoha_

    dyamanoha_

    Joined:
    Mar 17, 2013
    Posts:
    87
    Anyway, the important thing is the repro case. I actually got it captured

    upload_2023-2-27_20-44-54.png
     
  12. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    The HW Scheduler - Copy load wasn't there in the first picture, so with all the information you have given us so far, my guess is that you are creating a new texture or rendertexture every frame that you are not releasing. That will eventually force the GPU to move stuff over from VRAM to RAM.
     
  13. dyamanoha_

    dyamanoha_

    Joined:
    Mar 17, 2013
    Posts:
    87
    The game runs just fine at 60 fps with vsync on for extended periods of time. I ran it for a couple hours last night.

    If I flip vsync off, it will often repro within a couple seconds. There's also been a pattern where this will happen and then the frame rate recovers. If I watch system memory in the task manager for the game process it'll go from 500mb, spike upwards to 6gb but then come back down again.

    I agree there's obviously something going on with memory here, it does feel like a thrashing problem but I don't think I'm leaking render textures. I'm going to dig into this capture a bit more but I will look around for things like render texture abuse
     
    Last edited: Feb 28, 2023
  14. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    It is possible that the garbage collector can't keep up with the garbage created if the frame rate is very high but obviously, I'm just poking around in the dark.
     
  15. dyamanoha_

    dyamanoha_

    Joined:
    Mar 17, 2013
    Posts:
    87
    Yeah, the stabs are helpful so I appreciate it. I think this type of memory is also captured in the unity memory profile
     
  16. Nullrigin

    Nullrigin

    Joined:
    Nov 3, 2020
    Posts:
    6
    Hey dyamanoha_, have you found the cause of your issue?
     
  17. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,451
    I think the investigation continued over here.