Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

What means "MemoryManager.FallbackAllocation" in Profiler?

Discussion in 'Entity Component System' started by daschatten, Jan 13, 2020.

  1. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    Profiler shows a lot of MemoryManager.FallbackAllocations with my bursted path finding job. What do they mean?
     
    goldcloud144 likes this.
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    You allocating native containers inside job I guess
     
    Opeth001 and MNNoxMortem like this.
  3. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    Yes, i do this... thanks!
     
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,198
    It means the allocator you're allocating under are filled to the brim and you're falling back on slower allocators
     
    Baggers_, Egad_McDad and GilCat like this.
  5. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,630
    this code doesn't use jobs at all, why do I have this?

    upload_2020-6-23_18-4-27.png

    (it's doing a lot of compute.dispatch though)

    @MartinTilo
     

    Attached Files:

  6. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,198
    Don't know about compute.dispatch but are there any other Allocation markers in there? Nothing keeps you from e.g. allocating NativeArrays with the Temp allocator and running out space on that allocator. Whitin Jobs or outside like here.
     
  7. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,630
    I am pretty sure that that code doesn't use native containers, but I'll double check later.
     
  8. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,630
    @MartinTilo as you can see from this deep profile, the allocation comes from the internal unity API. You also notice that really would like to save that 1.20ms if it's caused just by it. Should we open a bug or something?

    upload_2020-6-24_8-50-17.png

    I have also notice that every combine dependency causes one of those

    upload_2020-6-24_8-52-24.png

    to be honest I feel disappointed that unity eats up precious ms in code that is supposed to be super optimized.

    if the solution is a simple as you say (the buffer is not big enough), it would be useful for the user to set up the max size of the buffer. Also the falling back to a slower allocation may need to be optional, as I may prefer actually to be warned about the temp buffer being full.
     

    Attached Files:

    Last edited: Jun 24, 2020
  9. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,198
    Yes please. Also maybe additionally raise this particular issue in a new thread on the Graphics subforum, as I have not enough insight into those APIs to provide much more info on this.
     
  10. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    910
    I get MemoryManager.FallbackAllocations when I update systems several times in one frame.
     
  11. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,630
    Got the info I was looking for, thanks anyway!
     
    Last edited: Jun 30, 2020
  12. hidingspot

    hidingspot

    Joined:
    Apr 27, 2011
    Posts:
    87
    Can you share your insights?
     
  13. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,630
    there is nothing special to share, only confirmation that I am not crazy at the moment. I found out that I can set the temp buffer size with Profiler.SetTempAllocatorRequestedSize and that doesn't solve my problem. So there is a bug, but I can't pin it down. I am going to open a bug report.
     
    Last edited: Jun 30, 2020
  14. Scoth_

    Scoth_

    Joined:
    May 25, 2020
    Posts:
    9
    I found a temporary solution to the problem. If anyone has the same problem of appearing several warnings like this in the profiler, read this below.



    It started to happen to me, for no reason, from one play to another and it hasn't stopped anymore
    All scripts that allocated memory got this warning MemoryManager.FallbackAllocation
    The strange thing is that I tested it on 3 different Unity versions and after some time everyone started to do that.
    I thought the problem was with a specific script because more errors appeared than the others, but I now found out that it's a problem in the project.


    Solution I discovered, just reimport everything. Right click in Project window --> Reimport All

    upload_2021-9-6_22-14-53.png

    This error will probably come back at some point, but I won't need to create a new project every time to solve the problem.

    If anyone knows the final solution please let me know, before the project becomes huge and takes too long to re-import.

    I'm using Entities 0.17.0-preview.42 and the problem occurred to me in versions 2020.3.13f1, 2021.1.19f1 and 2021.2.0b9.
    It will probably happen in all other versions because I always install a different one and it happens too.


    Just one more detail, this message appears in the profiler in EVERY place that allocates memory, not just in the ECS package scripts. Also appears in all messages: "Allocator Name: ALLOC_TEMP_MAIN".

    If anyone knows the solution I appreciate it. Sorry for any grammatical errors, I'm using a translator :)
     
  15. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,630
    Hi,

    Eventually the Unity guys helped me find the root of this problem. The issue starts when native memory leaks are accumulated. Unluckily, in my case, I was using a custom data structure that was not detected by the DOTS memory leaking system, however, even if these data structures were using the tempjob allocations, the symptom was the profiler getting confused and outputting the error we know about.

    Once I removed all the leaks, the red lines got away.

    To be clear: the memory fallback allocation problem is not due to temp allocations at all and it has nothing to do with it anyway. It's the profiler that somehow breaks when tempjob leaks are accumulated, showing something completely misleading.
     
    Last edited: Sep 8, 2021
    Scoth_, Lukas_Kastern and Baggers_ like this.
  16. pikminscience

    pikminscience

    Joined:
    Mar 26, 2019
    Posts:
    36
    Just had a similar issue to this. I had just imported the collections package and started testing out jobs.

    Noticed that every animator was producing fallback allocations under them (under every animator step). Restarted Unity and everything seems OK now. Will look in to it more thoroughly if it pops up again and post back.
     
  17. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,630
    it may pop back and if it does is because of memory leaks happening somewhere else (in my case because of me forgetting to free tempjob memory). As I said the profiler is reporting something completely wrong (and indeed impossible as I know that that code is not using temp allocations), Unity should treat this profiler issue as a bug.
     
  18. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,198
    Granted, the Profiler isn't really great at helping you figure out where it comes from yet. I have some ideas of how we could illuminate that more and what kinda tech is needed for it. It's not really trivial but some of that is already in the works, if a bit on a back burner right now.

    I'd be rather concerned if the profiler was "actively lying" about it though. I'd need to see some code that triggers this sample while not using temporary memory to be able to determine if this is a Profiler bug or just code that triggers temp allocations in the native code though. And, a lot of our native code is using temp memory, as it needs to be fast. So, right now I'd be more likely to assume that that is the case but willing to be proven wrong, and had a couple of thoughts of how the emission of some ProfilerMarkers might trigger temp allocations themselves. I don't see any such markers in the screenshot though so, not sure how that would happen yet.

    I guess you could try to put a ProfilerRecorder to the task of recording the occurence of
    MemoryManager.FallbackAllocation
    per frame. Then you don't need to be profiling the build while analyzing this, i.e. other samples are not emitted and the Profiler wouldn't be making any allocations that would trigger the fallback allocator. If it still fires, chances then are about 99% that it isn't the Profiler being wrong about it, but the code actually triggers it, however many levels deep in the call stack.
     
    apkdev likes this.
  19. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,630
  20. Dale_Kim

    Dale_Kim

    Unity Technologies

    Joined:
    May 8, 2019
    Posts:
    54
    I should clarify that the profiler isn't lying about the fallback allocations. Those fallback allocations are indeed happening, but it may not be clear exactly where or how it occurs. Usually, they occur because someone forgot to free an allocation from the Allocator.TempJob allocator. If the leak starts to accumulate, it can exhaust Allocator.TempJob's fast memory arena and start to use the fallback instead to satisfy allocation requests. These fallback allocations are costly and that's what's being reported in the profiler. The way to fix this is to find the leak and plug it.

    Most of the time, the leak is from forgetting to dispose a native container or forgetting to call UnsafeUtility.Free() on native memory that you explicitly requested. Usually, forgetting to dispose a native container is not a big problem since we have leak checking built in to Collections native containers to help you find those leaks. But if you have used UnsafeUtility.Malloc() to get memory on your own, there is no built in leak checking for that so you have to be very careful to UnsafeUtility.Free() everything. Finding this kinds of leak is very challenging at the moment and we are currently working on a solution to make it much easier to find these.

    Another possibility is that Unity's code is leaking somewhere and as a side effect, your allocations may be hitting the fallback because the allocator is full. In these cases, the problem is still fundamentally the same but you may have less visibility into where the leak is originating from. This is a super frustrating case and we're working on finding these leaks and fixing them so they don't affect customers.

    One diagnostic check to see if you're running into memory leaks is to see if you have this message in your log:
    Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
    If you have that, you are almost certainly running into this problem due to memory leaks and should start looking for any cases where a dispose or UnsafeUtility.Free call is missing. Unfortunately, if you don't have full source code access, you won't be able to get much help in terms of finding where the leak is originating from (this is what we're improving right now).

    Finally, another way you can get fallback allocations without a leak is if you happen to be allocating memory that's too big for Allocator.TempJob to satisfy. For example, if you're allocating megabytes of memory in one allocation, then it's unlikely to fit in the fast arena that Allocator.TempJob maintains so it is forced to fallback allocate. These cases are uncommon and usually don't fill the profiler timeline the same way as having a leak because large, multi-megabyte allocations are done seldomly. But it's still worth checking to see how much memory you're requesting to see if this is the reason why the fallback allocation is done. Usually, the cutoff is 1 MB before you start to use the fallback allocator.
     
  21. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,046
    @Dale_Kim Btw do I need to free allocation manually by calling Dispose API from the Allocator.Temp allocator just like Allocator.TempJob allocator? From what I know Allocator.Temp will auto dispose itself every frame.
     
  22. Dale_Kim

    Dale_Kim

    Unity Technologies

    Joined:
    May 8, 2019
    Posts:
    54
    You do not need to manually Dispose() those, but I prefer to always call Dispose() even if it's Allocator.Temp because it makes it easier to switch to other allocators which are not automatically disposed.
     
    Scoth_, apkdev and optimise like this.
  23. Scoth_

    Scoth_

    Joined:
    May 25, 2020
    Posts:
    9
    @Dale_Kim If simply restarting or reimporting the project solves the problem temporarily, is there any way to get the warnings back quickly and purposefully? something like lowering the necessary leak threshold to trigger these warnings, so you can find the leak location quickly after changing the code and starting the game a few times. In my case, the delay in starting to receive these warnings in Profiler (until the memory doesn't completely accumulate) is what makes it difficult to detect the problem location. I would also like to know if after solving the problem these warnings will automatically go out without having to reimport the project or clear the memory, so that I don't keep searching endlessly for solutions in useless places, something like a button to clear accumulated memory.
     
  24. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,630
    @Dale_Kim I still don't understand how you can be right. GPUInstancerManager.LateUpdate is a plugin, written purely in c#, that doesn't have any DOTS related code or use native memory. I fail to understand how FallbackAllocation can happen then. Please refer to the screen shot below.



    To be clear: you are right about the memory leak problem, but I cannot see how the profiler is NOT lying in the case above. Why is it reporting FallbackAllocation issues on code that doesn't use native memory at all?

    The only explanation I have is that some API, like the Graphics class, now uses native memory too.

    However, one note I have to add is that

    Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak

    doesn't happen all the times a leak happens, it stops spamming the console after few frames, which led us to think it was a glitch more than a permanent issue.

    By the way, since we fixed our leaks the problem is of course not occurring anymore.
     
    Last edited: Oct 1, 2021
  25. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,198
    I've just gone through the Project code on the bug report, sifting through the GPUInstancerManager.LateUpdate code and all the managed code it is calling, following the paths into Unity Native code where it enters that. It's a bit time consuming to do this and I can't get it to run as I had to comment out a bunch of stuff, just to get it to compile again on a newer Unity version. Therefore, I've decided to stopped at a call to
    ShaderVariantCollection.Add
    which does allocate Temp memory in Unity native code.
     
    sebas77 likes this.
  26. Dale_Kim

    Dale_Kim

    Unity Technologies

    Joined:
    May 8, 2019
    Posts:
    54
    @sebas77 Even if you aren't using DOTS or directly allocating native memory, any calls into the engine may make one of these TempJob allocations that can hit the fallback allocator. I'll take another look at your bug report to see if I can find where they actually come from for that section of code. Is the plugin source code a part of the project in the bug report?
     
    sebas77 and MartinTilo like this.
  27. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,198
    It's the GPUInstancesManager.LateUpdate and I've just checked it already. Lots of calls into native. I haven't checked how many of these trigger Temp Allocs, as I've stopped after hitting the first one.
     
    sebas77 likes this.
  28. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,630
    Ok I assume when you say lots of calls in to native you mean through the unity API, that's what I expected and it explains it.

    Thank you a lot for looking into it and confirming it.
     
    MartinTilo likes this.
  29. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,198
    Yes, most of the Unity API calls outside of packages still go relatively directly into Unity native code.
     
  30. Dale_Kim

    Dale_Kim

    Unity Technologies

    Joined:
    May 8, 2019
    Posts:
    54
    @sebas77 I looked at your project in the bug report again but I didn't see any fallback allocations in the GPUInstancerManager profiler markers. Is there something else I need to do in order to see it?
     
  31. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,630
    sorry I am not sure anymore, you may need to place some blocks to see it happening. However, I am satisfied with the theory that unity native code may use temp allocations. I previously thought it was a DOTS thing only.
     
  32. zhoutaosheng

    zhoutaosheng

    Joined:
    Aug 9, 2018
    Posts:
    1
    The reason is:
    In Unity, we use stack allocator to allocate memory that would be used in a function or in serveral frames. It's super fast, but the memory size is limited to 128KB~1MB according to different platforms. When you allocate more than 128KB~1MB, Unity would use FallbackAllocator, which is much slower than stack allocator, and you would see MemoryManager.FallbackAllocation in profiler.
     
  33. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,198
    Yeah, those Allocators have been around in Unity's native code since before DOTS to optimize memory usage & allocation speed based on different use cases. DOTS just exposed a few of them to managed land.