Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

[Fixed in 2021.2] ShadowCaster2D memory leak?

Discussion in '2D Experimental Preview' started by AlexVillalba, Jan 5, 2021.

  1. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    Hi everybody, I'm using Unity 2020.2.1 on Windows 10 and something strange has just happened. I played the game in the editor (the scene has several 2D lights and shadow casters) and then I went to have lunch, without stopping it. When I came back I found that the occupied memory was over 1,6 Gb (too much for a 2D game with just a few assets) and that the game was jittering every 2 seconds. I took a look to the CPU usage in the profiler and found that the spikes were caused by a memory collection in the ShadowCaster2D.Update() method (>700ms). I also saw that the same method allocates 115KB every frame.

    I have attached a picture and the profiler data just in case it is useful.
    profiling.PNG
     

    Attached Files:

  2. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    When I stopped the game, the Editor kept jittering. May it be something related to how shadowcasters work in editor mode?
    profilingEditor.PNG
     
  3. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    I don't know whether it is related, but I see in the Update() code a lot of calls to GetComponent which, in case of returning null, make unnecessary memory allocations AFAIK, which makes TryGetComponent a better option. Anyway I don't think this can generate 100KB of garbage.
     
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,461
  5. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    No, but I will try :)
     
  6. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    I've enabled GCAlloc callstacks and Deep profiling. The call stacks did not provide useful information but the Deep profiling did. It appears I was right, although I thought it was not possible, the problem is the GetComponent calls.

    profilingDeep.PNG

    There you can see 2 allocations done in GetComponent: one of 78.4KB and other of 37.2KB.
     
  7. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,461
    The information provided by the call tacks feature is more detailed than what you can see with Deep Profiling. Did you check the details view as mentioned in the documentation?
     
  8. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,461
    Hierarchy view collapses samples occuring in the same scope. That's 124 and 67 calls to
    GC.Alloc
    for those two. The multiple KB are divided across these. The details view:
    Show Related (Object) Data
    shows each Allocation occuring here with it's size and call stack. Alternatively the Raw Hierarchy also doesn't collapse samples.

    These allocations are actually already filtered from view for you here, see the
    EditorOnly [GetComponentNullErrorWrapper]
    sample and the manual section on Editor-Only samples
     
  9. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    Hi, yes I did. But this is what I get, am I doing something wrong?

    profilingCSALLOC.PNG
     
  10. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,461
    Oh wait, sorry no I'm wrong in this case. You only have insights into managed calls in the Callstack and the GetComponent samples come from the native code. It usually suffices and gives more details if the memory is allocated in managed code. BTW, in 2021.1 I've changed this view to by default not show these memory addresses and near empty lines as those are usually not that actionable unless you are doing some deep debugging with source code access.
     
  11. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    Ok no problem, thank you for the time you are spending on this, I'm going to build the project and profile without the editor, just to discard stuff.
     
  12. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    It seems to be an Editor issue, it does not happen in the build. The ShaderCaster2Ds are not allocating memory.
     
    MartinTilo likes this.
  13. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    I'm attaching a sample project that reproduces the problem.
     

    Attached Files:

  14. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,461
  15. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    Hi Martin, I've already reported it. And included also a link to this thread in the report. :)
    They could not reproduce it so I've sent them the same project.
     
    MartinTilo likes this.
  16. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
  17. AlexVillalba

    AlexVillalba

    Joined:
    Feb 7, 2017
    Posts:
    346
    UPDATE: Unity programmers have fixed the issue, I received the following by email:

    "A fix for this issue is now available on Universal RP package 12.0.0 and above. This package version is available in Unity 2021.2.0a9 and above."

    Thank you!
     
    NotaNaN and MartinTilo like this.
  18. laserDogRob

    laserDogRob

    Joined:
    Jun 20, 2018
    Posts:
    39
    I would be WONDERFUL if this was back-ported (is that the right term?) to 2020.2

    Thanks for the fix :)
     
  19. DanielTanBK

    DanielTanBK

    Unity Technologies

    Joined:
    Aug 20, 2019
    Posts:
    83
    Hello, we will take a look at backporting this fix to 2020.3
     
    laserDogRob and MartinTilo like this.
  20. laserDogRob

    laserDogRob

    Joined:
    Jun 20, 2018
    Posts:
    39
    That would be amazing, thank you.
     
  21. lorenzoteslermabe

    lorenzoteslermabe

    Joined:
    Dec 23, 2020
    Posts:
    24
    Hey, sorry to tag on here but what about 2019 LTS? Is this version left out for good in terms of URP fixes? We're still having 2D lights allocating garbage every frame :(
     
  22. spacefudge

    spacefudge

    Joined:
    Feb 1, 2021
    Posts:
    8
    Was this ever backported, I am using 2020.3.29 and can still reproduce
     
  23. DanielTanBK

    DanielTanBK

    Unity Technologies

    Joined:
    Aug 20, 2019
    Posts:
    83
    Hello, a backport was released for Universal RP package 11.0.0 (Unity 2021.1) and a pending one for Universal RP package 7.8.0 (Unity 2019.4)
    However, there does not seem to be one for Universal RP package 10.x for Unity 2020.3, we will open another request for this.

    You can refer to this issue tracker link to see the status of the backports
     
    MartinTilo likes this.