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

Memory Profiler: NativeArray, NativeList etc

Discussion in 'Profiler Previews' started by Carpet_Head, May 29, 2019.

  1. Carpet_Head

    Carpet_Head

    Joined:
    Nov 27, 2014
    Posts:
    258
    The memory profiler doesn't seem to show any memory created using NativeArray/NativeList - which are drastically lacking in useful ways to track memory usage/leaks. Is this supported and I am missing something or is there a specific view or option to use? I have hundreds of MB of NativeArray data that is completely absent from the memory profiler
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,451
    Hi,
    Right now, the best view for Native Array data is the Memory Map view, which also has a visual diff view when comparing snapshots.
    That said, DOTS was not much of a concern when we started on this project and we're looking at different ways to improve what the Memory Profiler has to offer for DOTS and Native Arrays. You can help inform how we tackle this:

    What problem are you encountering, that you are trying to tackle using the Memory Profiler?
     
  3. Carpet_Head

    Carpet_Head

    Joined:
    Nov 27, 2014
    Posts:
    258
    I spent the day trying to debug some issues with NativeLists that were getting wildly out of hand, and consuming crazy amounts of memory. We didn't lose references to them, so they were not "leaked" but the capacity had grown to very large levels and really inflated our memory usage. I hoped that the memory profiler would give me some clues in to what on earth was going on, as at the beginning it was not even clear where the memory was coming from. When I began, I did not even know where the memory was coming from

    When I first jumped into the memory profiler, it was as if they didn't exist - there didn't seem to be any clear view to see them at all, so at first it misguided me into thinking that no memory was being taken up by NativeList/NativeArray collections
     
  4. CPlusSharp22

    CPlusSharp22

    Joined:
    Dec 1, 2012
    Posts:
    111
    I don't like necroing threads but I came into this as well over a year later.

    I noticed that NativeArrays are hard to track down allocations. I have a keybind command that initializes a couple hundred of them with Allocator.Persistent in a single frame, and the profiler doesn't show any of them being allocated for in the frame. The profiler window's memory view just shows 1MB being allocated in the frame in both editor and windows standalone, but there's definitely more than that being allocated and used with NativeArrays. Unity memory will jump 0.50GB total when I do this in a single frame.

    But if I use the dedicated MemoryProfiler for some reason, it doesn't show up in the tree map. I have to go into Table -> Alloc -> ByNativeObject and expand by N/A. Of which, it doesn't really give me useful info.

    I really wish we could give these things markers or have some way of tracing them. I know they definitely store trace calls somewhere when you turn on Full Stack Leak Detection in the editor. I'm using 2019.4.3f1 by the way.
     
  5. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,451
    No worries on reviving this thread, it's sadly still relevant.

    I totally get the point on that Memory being somewhat hidden in the tooling, where the main and most visual views focus too much on memory related to Managed or Native Objects (the UnityEngine.Object kind), without being very explicit about that or hinting that there is Memory to be found outside of those two broad categories.

    We have some ideas on improving that situation, both by a continuous memory capture support and adding call stacks reporting to these allocs. That would also require new or adjusted views and we should also just generally provide a better landing page for Memory snapshots that provides a more holistic overview of what has been captured.
    We'll be getting to that but I can't give an ETA yet, as there's other work that needs to get tied up first.

    How would you imagine that to look like? Would this be beyond the Profiler Marker scopes they get allocated in, that would be reported via call stacks? It might admittedly be nice to be separate from that, if call stacks reporting is opt-in, so you get some context even without reporting on. What kind of context information would you be interested in sending along and at which points in the Native container's lifecycle?
     
  6. CPlusSharp22

    CPlusSharp22

    Joined:
    Dec 1, 2012
    Posts:
    111
    Glad to know it hasn't been forgotten! Thanks for the speedy reply.

    Just like a ProfilerMarker I'd expect to be able to pass in a string or maybe some kind of integer id the user can just map out themselves (since I know jobs can be picky even with static strings in profilermarkers). And when we see it in the profiler appear like a profilermarker with that identifier, we can simply see what size it was initially allocated for. NativeLists is probably a bit trickier to track, but at least Arrays are fixed. If it's a release build, compile it out conditionally.

    I was thinking of making my own static API for allocating and tracking them, just like a profilermarker would, for the time being and either dump the data it's tracked on command or maybe even just leveraging a profilermarker name somehow (even if it means paying for the string manipulation for the marker to show the numbers).

    To give you an idea of a problem I'm trying to track down: I'm noticing that when I fire my command that's allocating a bunch of fragmented/different NativeArrays (persistently),that Unity is reserving upwards of 0.50GB memory, but I'm expecting only 100MB at most. The Profiler window, if I take a sample directly in that, before and after shows NativeArray size exploding from modest 80MB to upwards of 0.50GB. The same in the Memory Profiler window if I drill down into the tables.
    I'd expect 180MB but instead I'm seeing 4x that and I have no idea why or where. Obvious assumption is that my math is horribly wrong in one of these allocations. Identifying where though is the troublesome bit. I can see where profiling is taking the longest, but the math checks out (its just a lot of small allocations that only add up to a couple of MB). The fastest and "truest" way to fix what I'm investigating (and to speed it up performance wise) is just making an arena/contiguous chunk allocator that's pre-allocated from the beginning of the scene, but it would be nice to know where I went wrong originally lol