Overview
In game development, "creating a fun game" is equally important as "creating a game that runs smoothly." Frame rate drops (stuttering) and crashes from excessive memory usage severely damage player experience. To solve performance issues, you must first accurately identify "what is slow, where, and why." Unity's built-in Profiler is the most powerful tool for this.
The Profiler collects various performance data during gameplay—CPU usage, memory allocation, rendering, physics—on a per-frame basis, displaying them as graphs and detailed lists. This enables developers to identify bottlenecks based on data rather than guesswork, allowing targeted optimization.
This article covers basic Profiler usage and how to read key modules for discovering common performance problems.
Basic Profiler Usage
-
Open Profiler Window: Select
Window > Analysis > Profilerfrom the menu. -
Select Profiling Target: Use the
Playmodedropdown at the top to select what to profile.Editorprofiles games running in Unity Editor; selectingEnter IPor connected device names profiles development builds running on actual devices (PC, mobile, console). For accurate performance measurement, always profile on target hardware. -
Start Recording: Confirm
Recordbutton is on and play the game. Module graphs begin updating in real-time. -
Select and Analyze Frames: Click frames with spikes (prominent peaks) or particularly heavy processing on the graph. Detailed processing information for that frame appears at the bottom of the window. Drill down to see specific information like how many milliseconds each method call took.
Key Profiler Modules
The left side of the Profiler window lists modules for different analysis needs.
CPU Usage
The most commonly used module. Shows what the CPU spent time on each frame.
- Timeline View: Shows processing for each thread (Main Thread, Render Thread, etc.) chronologically. Helps grasp which processing is extending total frame time.
- Hierarchy View: Displays all method calls within a frame hierarchically. Sort by
Time msorSelf ms(that method's own processing time) to identify the most time-consuming processes.- Common Bottlenecks: Heavy processing running every frame in
Update(), callingGetComponent()in loops, complex physics, inefficient AI logic, etc.
- Common Bottlenecks: Heavy processing running every frame in
GPU Usage
Shows what the GPU spent time on for rendering. Useful when GPU is the bottleneck (GPU Bound). (Available only on some platforms)
Rendering
Shows what draw commands the CPU sent to the GPU.
- Batches: Draw call count. Too many increase CPU load. Optimize for static/dynamic batching by sharing materials.
- SetPass Calls: Shader/material switch count. Also causes CPU load.
- Triangles / Vertices: Polygon/vertex count displayed in scene. Too many increase GPU load.
Memory
Shows how much memory the game allocates and uses.
- Simple View: See overview like
Total Reserved MemoryandUnity(memory used by Unity engine). - Detailed View: Press
Take Sampleto capture a snapshot of all assets and objects in memory at that moment. Very powerful for identifying which textures, meshes, or audio clips are consuming memory. Common causes: textures with excessive resolution or improper compression settings.
GC (Garbage Collection)
In CPU Usage Profiler, you may see GC.Collect appearing as large spikes. This means Garbage Collection (GC) occurred. GC is a convenient mechanism that automatically cleans up unused memory, but game execution temporarily pauses (stutters) during processing, causing player-perceptible stuttering.
The main cause of GC is unnecessary memory allocation in Update(). For example:
- String concatenation (
string a = "hello" + "world";) - Class instantiation with
newkeyword - New array creation (
new Vector3[10])
Doing these every frame generates lots of small garbage, causing frequent GC. Focus on the GC Alloc column, identify processing that allocates memory every frame, and reduce it as much as possible—this is key to suppressing GC spikes.
Summary
The Profiler is like a "stethoscope" or "X-ray" for performance optimization.
- Don't optimize by guesswork. Always measure with Profiler and identify bottlenecks based on data.
- For accurate measurement, profile on actual devices whenever possible.
- Use
CPU Usagemodule to identify time-consuming methods. - Use
Renderingmodule to check batch count and polygon count. - Use
Memorymodule to identify memory-consuming assets. - Focus on
GC Allocand reduce unnecessary memory allocations to prevent GC spikes.
Performance optimization is painstaking work, but mastering the Profiler lets you efficiently discover problems and deliver comfortable gaming experiences to players.