Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Memory Profiler: Data agglomeration

Discussion in 'Editor & General Support' started by sebas77, Jul 1, 2020.

  1. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,220
    It's several weeks that I have been wondering why our game was taking 2.6GB while it always been around 1.3GB. Memory Profiler didn't show up anything weird so I couldn't figure it out.
    By chance, I started to profile with Rider, but for other performance reasons, not memory allocation. Fate is that Rider is able to trace also allocations and it showed a 1.2GB of allocation in one constructor, which led to the real problem. We were allocating unreasonable buffers for networking.

    I started to investigate why the Memory Profiler failed to highlight the problem. Of course, the approaches are different, Rider got there just tracing the calls, but the Memory Profiler, with its GC crawling, should be more powerful than that!

    Well turns out that the 1.2GB were allocated over 1100 arrays, so the Memory profiler was not seeing the problem. Of course, you need memory data agglomeration, but I have been told you are working on it.

    This is thread is proof that the feature is not a joke and I am looking forward for it.
     
    AndyBarnard likes this.
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    896
    Hi and thank you for the feedback. We are indeed planning on improving the Memory Profiler, among that summing up the total Memory usage and different break downs of the biggest potential issues or areas to focus on. I guess that should help in your case too.

    However, I'd like to understand your case a bit better because if all those arrays had the same type, I think the Memory Map should have already shown that Type as a big-ticket item that should have certainly drawn attention to itself.

    So I guess they were arrays of different types? I have some ideas on how to address this or similar issues but I'd just like to make sure I understand it properly first :)
     
  3. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,220
    Hello, no they were actually of the same type. I show you the code:

    Code (CSharp):
    1.  public NetworkReceiver(uint bufferSize, uint poolSize, NetworkReceivers networkReceivers) : base(networkReceivers)
    2.         {
    3.             _buffers = new Buffer[poolSize];
    4.             for (var i = 0; i < poolSize; ++i)
    5.             {
    6.                 _buffers[i] = new Buffer {Data = new byte[bufferSize]};
    7.             }
    8.  
    9.             _readHead = 0;
    10.             _writeHead = 0;
    11.         }

    However this is a constructor of a generic class

    public class NetworkReceiver<T>

    that was called with 11 different Types

    and in one case poolSize was 100 and bufferSize was 6M (so that case only was already taken more than 600MB)

    All that said, there are several ways to solve the problem. One could be to agglomerate it per type, but I didn't find the option in the memory profiler.

    The other one could be to see a hierarchical allocation following the object hierarchy in the GC heap.
     
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    896
    Thank you for the example. I'm assuming Buffer is not generic and this should have all shown up as byte[] data?

    The Tree Map of the Memory Profiler package does just that. Did you mean the Memory Profiler module in the Profiler window before?

    Yes, something like this was on my mind too, though we can only do this for objects that only have 1 other object referencing them, because otherwise it's unclear who "owns" that Memory. There's other things we can do too, like tracking the Allocation site and grouping based on that but that's likely a different mode.

    Anyways. All those are under investigation/work for improvement within the Memory Profiler package. Your feedback is much appreciated and we'll take it in to the work we do there :)

    If you have any further feedback on the package, you can also drop it into the Profiler Previews subforum and/or tag it with "profiling" ;).
     
  5. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,220
    Tree map was showing this:



    I was looking for that forum but didn't find it
     
  6. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    896
    Yeah it's a bit hidden in the beta & experimental features subforum.

    As discussed on Twitter, this looks like a bug in the crawler. That view should definitely have shown a huge lump of byte[] data...

    I'm assuming you're on the latest version of the package right?
     
  7. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,220
    yes I am. If you want to you can send me a preview of the new one so I can check it.
     
unityunity