In this lesson, you will learn how to use Unreal Insights to transform raw performance data into actionable insights for optimizing your C++ containers.
Even when you follow best practices like using TArray or TSet, your game’s performance can still suffer from "hidden" costs like frequent heap reallocations or cache misses. Unreal Insights is the professional tool used to peer inside these operations to see exactly how much time your code spends managing memory.
Capturing the Data
To start, launch your game with the -trace=cpu,mem command-line arguments. This instructs the engine to record both the time spent in functions (CPU) and the allocation patterns (Mem). As you trigger the logic using your containers—such as a loop that frequently adds or clears elements—the engine captures these events in a .utrace file.
Identifying Bottlenecks
Once you open the trace in the Unreal Insights tool, focus on the Timing Insights tab. Look for spikes in your game’s frame time during operations involving large TArray or TMap structures.
If you see deep stacks of FMemory::Realloc occurring inside your game loops, you’ve found a smoking gun. This usually indicates that a container is being resized repeatedly because it wasn't pre-allocated. You can confirm this by checking the Memory Insights tab to see if your container is triggering "allocation churn," which causes performance stutters.
Validating Your Fix
After identifying an inefficient container, you can apply an optimization, such as using Reserve() for a TArray or switching to TInlineAllocator to keep data on the stack rather than the heap.
// Before: Frequent reallocations
TArray<FVector> PathPoints;
for(int32 i = 0; i < 1000; ++i) { PathPoints.Add(CalculatePoint(i)); }
// After: Reserving memory to prevent reallocations
TArray<FVector> PathPoints;
PathPoints.Reserve(1000);
for(int32 i = 0; i < 1000; ++i) { PathPoints.Add(CalculatePoint(i)); }
By profiling again, you should see the FMemory::Realloc calls vanish from the trace, providing visual proof that your memory optimization successfully improved your frame consistency.
Key Takeaway: Use Unreal Insights to identify heap reallocations and memory churn, then validate your container optimizations by verifying that costly memory operations have been removed from your timing traces.