How Can You Track Mono Heap Allocations on Unity Worker Threads?
Moving non-Unity API work to worker threads is a common way to reduce main-thread overhead and improve frame rate. However, tracking heap allocations from these threads can be difficult because Unity Profiler does not expose their GC.Alloc data in the same way as main-thread allocations. GOT Online's Mono analysis now provides multi-thread allocation tracking, making it easier to identify hidden allocations, investigate heap growth, and find potential memory leaks.
About GameOptim GameOptim helps Unity developers identify memory issues, rendering bottlenecks, and performance regressions through automated profiling and cloud based performance analysis. Explore more: 🌐 Website: www.gameoptim.com https://www.gameoptim.com/?fopt=blog 📘 Blog: www.gameoptim.com/blog/ https://www.gameoptim.com/blog/ 💼 LinkedIn: www.linkedin.com/company/gameoptim/ https://www.linkedin.com/company/gameoptim/ 🎥 YouTube: GO.PerformanceLab https://www.youtube.com/@GO.PerformanceLab 💬 Discord: GameOptim https://discord.gg/4Jh6hj9gRw ⭐ GitHub: GameOptim https://github.com/GameOptim/unity mobile performance guide 💻 Dev: GameOptim https://dev.to/gameoptim Why Do Worker Thread Allocations Matter? Offloading CPU intensive work to worker threads can help reduce the workload on Unity's main thread. In many cases, operations that do not depend on Unity APIs can safely be moved away from the main thread. However, moving the work to another thread does not eliminate its memory cost. For example, a component may start two worker threads in and periodically allocate managed memory from those threads. https://uwa overseas public.oss us east 1.aliyuncs.com/uploads/markdown/7c987963 2483 4b75 81fa a558909f6bfa.png These allocations still contribute to the application's managed heap and can cause the same problems as allocations made on the main thread: Frequent allocations can increase GC pressure and trigger more frequent garbage collection. Excessive temporary allocations can increase managed heap usage. Objects that remain referenced longer than expected can cause continued heap growth and potential memory leaks. The challenge is that these allocations are not always visible in Unity Profiler's standard data. Why Doesn't Unity Profiler Show These Allocations? When managed memory is allocated from a worker thread, Unity Profiler may not provide the same level of allocation visibility that it provides for the main thread. This makes it difficult to answer questions such as: Which worker thread function is allocating memory? How much memory is being allocated? How frequently is the allocation happening? Which object types are responsible for the allocation? Is the allocated memory eventually released by the GC? Without this information, developers may know that managed memory is growing but still have difficulty identifying the actual source. How Does GOT Online Track Non Main Thread Allocations? GOT Online's Mono analysis mode adds multi thread allocation tracking to help identify these otherwise hard to see allocations. In the assessment report, open Specific Heap Allocations . Functions that perform managed heap allocations from non main threads are displayed with a prefix. The prefix indicates that the function was called from a non main thread and performed a managed heap allocation. You can select any of these functions to inspect its detailed heap allocation information. This makes it possible to connect a worker thread operation with the managed memory it allocates, rather than treating the heap growth as an unexplained increase. How Can You Find Worker Thread Memory Leaks? Allocation tracking is only part of the problem. The next question is whether the allocated objects are eventually released. If objects remain referenced and cannot be collected by the garbage collector, the corresponding heap usage may continue to grow. GOT Online's Heap Memory Leak Analysis can be used to investigate this behavior. The corresponding functions can be located in the leak analysis panel, where you can inspect their heap growth over time. You can also select two data points and compare them to determine how the heap changed between different test stages. This is particularly useful when investigating gradual heap growth that cannot be explained by a single allocation spike. How Can You Identify the Allocated Object Types? After locating a suspicious function or heap growth point, click the blue analysis button on the right side of the report. GOT Online will show the corresponding managed object types, along with: Object type Allocation size Allocation count This provides a more detailed view of what is actually consuming managed heap memory. Instead of stopping at "the worker thread is allocating memory," you can continue down to the object level and determine which allocations deserve optimization. A Practical Workflow for Debugging Worker Thread Allocations A practical workflow is: 1. Identify the worker thread allocation Check Specific Heap Allocations and look for functions with the prefix. 2. Inspect the allocation details Select the function to determine how much managed memory it allocates and how frequently the allocation occurs. 3. Check for persistent heap growth Use Heap Memory Leak Analysis to determine whether the corresponding memory continues to grow between measurements. 4. Compare different test points Select two nodes or data points to compare heap growth and identify changes introduced by a specific code path or optimization. 5. Inspect object types Use the detailed analysis button to identify the object types, allocation sizes, and allocation counts responsible for the memory usage. This workflow helps distinguish between normal short lived allocations and allocations that contribute to persistent heap growth. Key Takeaways Moving non Unity API work to worker threads can reduce main thread pressure, but it does not make managed memory allocations disappear. When analyzing Unity Mono memory: Worker thread allocations can still contribute to GC pressure. Unity Profiler may not provide sufficient visibility into these allocations. GOT Online's Mono mode can identify allocations made from non main threads. Functions with the prefix indicate non main thread heap allocations. Heap Memory Leak Analysis can help identify persistent heap growth. Object level analysis can reveal the types, sizes, and counts of the allocated objects. For projects that rely heavily on worker threads, tracking both CPU workload and managed memory allocation is important. Reducing main thread work should not come at the cost of introducing hidden allocation or heap growth problems elsewhere.