Record and view call stacks for Profiler samples
Record call stacks for GC.Alloc, UnsafeUtility.Malloc, and JobHandle.Complete samples to find the code that triggers them.
Read time 2 minutesLast updated 2 days ago
Use the CPU Usage Profiler module to record call stacks for , , and samples and find the code that triggers them.
GC.AllocUnsafeUtility.MallocJobHandle.CompleteWhenever a script creates a new managed object that doesn't fit in the current memory heap, Unity performs garbage collection. Garbage collection pauses all threads and scans the managed heap for unused memory. This process might take a long time and disrupt the frame rate. Managed garbage collection appears as samples in the Profiler, while managed allocations appear as .
GC.CollectGC.AllocTo prevent the garbage collector from affecting your application's frame rate and to keep the heap size small, try to keep the value at zero while your application runs. In the Timeline view, samples appear colored in red-magenta and represent the size of the allocation.
GC.AllocGC.AllocTo learn where in the code managed allocations happen, you can enable the full call stacks for . For more information about the managed heap, refer to the documentation on Managed memory.
GC.AllocEnable full call stacks
You can enable full call stacks for samples that , , and emit. To enable these call stacks:
GC.AllocUnsafeUtility.MallocJobHandle.Complete- In the Profiler window toolbar, enable Call Stacks. By default, this enables the call stacks for samples.
GC.Alloc - To enable other call stacks, select the dropdown arrow and enable any of the other markers you want to display the call stacks for.
You can use this functionality whether you profile in the Unity Editor or on a running Player. This only takes effect for the frames you profile after you enable call stacks.
View call stacks
To investigate call stacks:
- Select a ,
GC.Alloc, orUnsafeUtility.Mallocsample.JobHandle.Complete - In the Timeline view, a sample appears nested under the selected sample.
Profiler.Callstack - Switch to the Hierarchy view and set the Details dropdown to Related Data to view the full details of the call stack.