Introducing Owlcat Mono Profiler for Unity

Official part of the event

Good afternoon. I work as a programmer at Owlcat Games, which released one of the most successful Russian computer RPGs Pathfinder: Kingmaker and is currently working on its sequel, Pathfinder: Wrath of the Righteous. During the porting of the first game of our studio on the console, we faced the problem of finding memory leaks. For various reasons, the standard tools of the Unity engine and target platforms turned out to be not very convenient for dealing with leaks, and therefore we decided to write our own tool, which I will discuss below.





Owlcat Mono Profiler is designed to investigate Mono memory usage in Unity games. It is available to everyone in the form of compiled binaries (under Windows) and source code on Github . Unlike the built-in Unity profiler, as well as the Memory Profiler package, it does not require taking snapshots of the memory state, but constantly monitors the Mono heap, which allows you to detect not only leaks, but also allocation peaks and redundant repeated allocations. Compared to platform-specific tools like Memory Analyzer for PS4, it correctly displays events that happen to garbage-collected memory.





At this point, we will end the formalities and move on to the cool story.





A fatal flaw in all other tools

It all started with the fact that we found out that the memory in our game is leaking. On a PC, this was not a problem, since it does not flow like a waterfall, and even on weak machines in our time there will be more memory than PlayStation 4 or XBox One. Plus, when Windows runs out of memory, it starts throwing the excess into a swap, and consoles simply kill your application, and go figure out where you screwed up.





The built-in Unity tools had to be swept away almost immediately: in Unity 2018.4 they actually did not work with our game (taking one snapshot of the memory state could take 8+ hours, but on PlayStation I never managed to wait for it in principle). It got much better in 2019.x, but we couldn't switch to it - changing the major version of the engine in Unity breaks too much.





PlayStation 4 Memory Analyzer. , , ( ). alloc/realloc/free , , memory pool' ..





. , Mono, , , BoehmGC. , , , - , . , .





Unity

, . Owlcat Games , , C++, - , , , , , . , - , , GC - .





, ? … ( , BoehmGC PS4). , - , . - , , gc_malloc__. . C++ - "". "", , . , , , . , --- ( - ) ", , , , ", . , - , , - , - .





BoehmGC : -, ( ) , -, , " ". BoehmGC, , , , , - , , , X, , , . , , , BoehmGC - PlayStation , ( BoehmGC, Mono).





.

, , . - , , , C++. : , il2cpp, , , , , - , … , , - , Unity , , PC, il2cpp .





, , Mono? , . Mono, Unity, . , , , , , , , - (, Mono Unity - , , PlayStation!).





!

, , Heap-Prof, Mono, , , . , , :





  • , , " ".





  • ( " ") , . , - " ".





, heap-prof dll, , GetProcAddress Mono, , … . mono_object_is_alive. , , , Mono, Massimiliano Mantione, Mono-dev 2009 . , heap-prof, , "The problem is that this is not reliable: "mono_object_is_alive" was not meant to be a public function. And in fact sometimes the heap snapshots are wrong (or the profiler crashes).". , API , SGen, Unity Mono …





( ) , : , mono_object_is_alive . - , , ?! , ( ).





, , , , . , . - - , - ? ? "" - , . , - (, , A B, A B, A - , B, ). , , .





BoehmGC, , - , , - . Mono , , . - … , .





" ! …"

. , , "- " . , , - , , . , .





, , , , , , , ( Unity).





, , , , . , , , , .





, , ( , ). PDB Unity Player: , , , , , , ( , ). , Unity , Microsoft Detours .





. , 20 , 5-10 ( ). , , , , : ~2 ~200Mb . /UI, , , , /UI ( ).





( ) .





, Qt5, ( , , , -, Microsoft). , SQLite ( ) , memory mapped database . Unity, , , ( , ), , , managed - .





. , - . , , , . ( -!) . , Unity, Owlcat Grooming Toolkit. CPU , dotTrace, .








All Articles