In this lesson, you will learn how to identify and analyze frame rate hitches caused by the Garbage Collector (GC) during runtime.
As your project grows, the number of UObject instances in memory increases. When the Garbage Collector runs, it must traverse the object graph to identify which objects are no longer referenced by a UPROPERTY pointer or the Root Set. If you have thousands of objects being destroyed at once, the GC process can take long enough to cause a visible "hitch" or stutter in your frame rate.
Using the Stat Commands
The first step in profiling these spikes is to visualize GC activity in real-time. Open your game in the Editor, press the tilde (~) key to open the console, and type:
stat gc
This displays the time spent in the current frame performing GC tasks. If you see spikes in the "GC Time" column that correspond with drops in your frame rate, you have confirmed that the engine’s memory management is the culprit.
Deep Dive with Insights
For more granular data, use the Unreal Insights tool. By capturing a trace while your game runs, you can view the "Garbage Collection" timing channel. This allows you to see exactly which phase of the GC is taking the longest:
- Reachability Analysis: Searching for objects that are still "alive."
- Purge/Destruction: Calling
BeginDestroyon objects identified as garbage.
If the Purge phase is the bottleneck, it often means you are destroying too many complex objects simultaneously. To mitigate this, consider "pooling" your objects—instead of destroying them, toggle their visibility or reset their state to be reused later. This keeps the object count stable and avoids the expensive overhead of constant destruction and reallocation.
By monitoring these spikes early, you can balance object lifetimes and ensure that the engine’s automatic memory management doesn't interfere with your game's fluid performance.
Key Takeaway: Use stat gc to detect spikes in real-time and Unreal Insights to pinpoint whether the bottleneck lies in reachability analysis or object destruction, allowing you to optimize your object lifecycle management accordingly.