Documentation

Unity Engine

Download Unity

Unity Engine

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
GC.Alloc
,
UnsafeUtility.Malloc
, and
JobHandle.Complete
samples and find the code that triggers them.
Whenever 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
GC.Collect
samples in the Profiler, while managed allocations appear as
GC.Alloc
.
To prevent the garbage collector from affecting your application's frame rate and to keep the heap size small, try to keep the
GC.Alloc
value at zero while your application runs. In the Timeline view,
GC.Alloc
samples appear colored in red-magenta and represent the size of the allocation.
To learn where in the code managed allocations happen, you can enable the full call stacks for
GC.Alloc
. For more information about the managed heap, refer to the documentation on Managed memory.

Enable full call stacks

You can enable full call stacks for samples that
GC.Alloc
,
UnsafeUtility.Malloc
, and
JobHandle.Complete
emit. To enable these call stacks:
  1. In the Profiler window toolbar, enable Call Stacks. By default, this enables the call stacks for
    GC.Alloc
    samples.
  2. 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:
  1. Select a
    GC.Alloc
    ,
    UnsafeUtility.Malloc
    , or
    JobHandle.Complete
    sample.
  2. In the Timeline view, a
    Profiler.Callstack
    sample appears nested under the selected sample.
  3. Switch to the Hierarchy view and set the Details dropdown to Related Data to view the full details of the call stack.
Note
In the Old Timeline (Legacy) view, you can hover over a sample and select Copy to copy details of the call stack.

Additional resources