Performance Profiling
Use this skill to diagnose Apple app performance issues systematically, pick the right profiling workflow, apply targeted fixes, and verify the change with real measurements.
Decision Tree
Choose the reference file before changing code:
What performance problem are you investigating?
+ App hangs, stutters, dropped frames, slow UI, high CPU
-> Read references/time-profiler.md
+ High memory, leaks, OOM crashes, growing footprint
-> Read references/memory-profiling.md
+ Slow cold launch, warm launch, resume, or time to first frame
-> Read references/launch-optimization.md
+ Battery drain, thermal throttling, background energy, network waste
-> Read references/energy-diagnostics.md
+ General "app feels slow"
-> Start with references/time-profiler.md, then references/memory-profiling.md
+ Pre-release performance audit
-> Read all reference files and use the review checklist below
Quick Reference
| Problem |
Instrument / Tool |
Key Metric |
Reference |
| UI hangs over 250 ms |
Time Profiler + Hangs |
Hang duration, main thread stack |
references/time-profiler.md |
| High CPU usage |
Time Profiler |
CPU percent by function, call tree weight |
references/time-profiler.md |
| Memory leak |
Leaks + Memory Graph |
Leaked bytes, retain cycle paths |
references/memory-profiling.md |
| Memory growth |
Allocations |
Live bytes, generation analysis |
references/memory-profiling.md |
| Slow launch |
App Launch |
Time to first frame, pre-main, post-main |
references/launch-optimization.md |
| Battery drain |
Energy Log |
Energy impact, CPU/GPU/network activity |
references/energy-diagnostics.md |
| Thermal issues |
Activity Monitor, Instruments |
Thermal state transitions |
references/energy-diagnostics.md |
| Network waste |
Network profiler |
Redundant fetches, payload size |
references/energy-diagnostics.md |
Workflow
- Identify the performance category from the user report, traces, logs, or code path.
- Read only the matching reference file unless the issue is broad or unclear.
- Prefer real device profiling with a Release build and representative data.
- Inspect the code path named by the profile before proposing a fix.
- Apply the smallest targeted fix that addresses the measured bottleneck.
- Re-profile or add a repeatable measurement to confirm the improvement.
Profiling Ground Rules
- Profile on device when possible; Simulator uses host CPU and memory.
- Use Release configuration because optimizations can change hot paths.
- Reproduce with representative data, not empty databases or toy assets.
- Close unrelated apps to reduce noise during profiling.
- Keep measurements before and after the fix so the outcome is concrete.
- Add
os_signpost markers when a workflow needs ongoing timing visibility.
Xcode Diagnostics
Recommend relevant Scheme > Run > Diagnostics settings when they match the suspected issue:
| Setting |
Use For |
| Main Thread Checker |
UI work off the main thread |
| Thread Sanitizer |
Data races and unsafe shared state |
| Address Sanitizer |
Buffer overflows and use-after-free |
| Malloc Stack Logging |
Allocation call stacks |
| Zombie Objects |
Messages to deallocated objects |
MetricKit Hook
Suggest MetricKit for production monitoring of launch, responsiveness, memory, and diagnostics:
import MetricKit
final class PerformanceReporter: NSObject, MXMetricManagerSubscriber {
func startCollecting() {
MXMetricManager.shared.add(self)
}
func didReceive(_ payloads: [MXMetricPayload]) {
for payload in payloads {
if let launch = payload.applicationLaunchMetrics {
log("Resume time: \(launch.histogrammedResumeTime)")
}
if let responsiveness = payload.applicationResponsivenessMetrics {
log("Hang time: \(responsiveness.histogrammedApplicationHangTime)")
}
if let memory = payload.memoryMetrics {
log("Peak memory: \(memory.peakMemoryUsage)")
}
}
}
func didReceive(_ payloads: [MXDiagnosticPayload]) {
for payload in payloads {
if let hangs = payload.hangDiagnostics {
for hang in hangs {
log("Hang: \(hang.callStackTree)")
}
}
}
}
}
Review Checklist
Responsiveness:
- No synchronous work on the main thread over 100 ms.
- No file I/O or network calls on the main thread.
- Large Core Data or SwiftData fetches use background contexts.
- Images decode off the main thread.
@MainActor is limited to code that truly needs UI access.
Memory:
- No retain cycles in delegates, closures, observers, or async tasks.
- Large resources are released when no longer visible.
- Collections and caches are bounded.
autoreleasepool is used in tight loops that create Objective-C objects.
Launch:
- No heavy work in
init() of the @main App struct.
- Non-essential initialization is deferred.
- Dynamic frameworks are minimized where practical.
- No synchronous network calls occur during launch.
Energy:
- Background tasks use the appropriate
BGTaskScheduler request type.
- Location accuracy matches the product need.
- Timers use tolerance so the system can coalesce wakeups.
- Network requests are batched and cached where possible.
References
references/time-profiler.md: CPU profiling, hang detection, signpost API.
references/memory-profiling.md: Allocations, Leaks, Memory Graph debugger.
references/launch-optimization.md: Launch phases and cold/warm start optimization.
references/energy-diagnostics.md: Battery, thermal state, and network efficiency.
1---2name: performance-profiling3description: Guide performance profiling for Apple platform apps with Instruments, Xcode diagnostics, and MetricKit. Use when investigating app hangs, stutters, high CPU, memory leaks, memory growth, OOM crashes, slow launch, battery drain, thermal issues, App Store performance readiness, or when adding os_signpost and measurement hooks.4---5
6# Performance Profiling
7
8Use this skill to diagnose Apple app performance issues systematically, pick the right profiling workflow, apply targeted fixes, and verify the change with real measurements.
9
10## Decision Tree
11
12Choose the reference file before changing code:
13
14```text
15What performance problem are you investigating?
16
17+ App hangs, stutters, dropped frames, slow UI, high CPU
18 -> Read references/time-profiler.md
19
20+ High memory, leaks, OOM crashes, growing footprint
21 -> Read references/memory-profiling.md
22
23+ Slow cold launch, warm launch, resume, or time to first frame
24 -> Read references/launch-optimization.md
25
26+ Battery drain, thermal throttling, background energy, network waste
27 -> Read references/energy-diagnostics.md
28
29+ General "app feels slow"
30 -> Start with references/time-profiler.md, then references/memory-profiling.md
31
32+ Pre-release performance audit
33 -> Read all reference files and use the review checklist below
34```
35
36## Quick Reference
37
38| Problem | Instrument / Tool | Key Metric | Reference |
39| --- | --- | --- | --- |
40| UI hangs over 250 ms | Time Profiler + Hangs | Hang duration, main thread stack | `references/time-profiler.md` |
41| High CPU usage | Time Profiler | CPU percent by function, call tree weight | `references/time-profiler.md` |
42| Memory leak | Leaks + Memory Graph | Leaked bytes, retain cycle paths | `references/memory-profiling.md` |
43| Memory growth | Allocations | Live bytes, generation analysis | `references/memory-profiling.md` |
44| Slow launch | App Launch | Time to first frame, pre-main, post-main | `references/launch-optimization.md` |
45| Battery drain | Energy Log | Energy impact, CPU/GPU/network activity | `references/energy-diagnostics.md` |
46| Thermal issues | Activity Monitor, Instruments | Thermal state transitions | `references/energy-diagnostics.md` |
47| Network waste | Network profiler | Redundant fetches, payload size | `references/energy-diagnostics.md` |
48
49## Workflow
50
511. Identify the performance category from the user report, traces, logs, or code path.
522. Read only the matching reference file unless the issue is broad or unclear.
533. Prefer real device profiling with a Release build and representative data.
544. Inspect the code path named by the profile before proposing a fix.
555. Apply the smallest targeted fix that addresses the measured bottleneck.
566. Re-profile or add a repeatable measurement to confirm the improvement.
57
58## Profiling Ground Rules
59
60- Profile on device when possible; Simulator uses host CPU and memory.
61- Use Release configuration because optimizations can change hot paths.
62- Reproduce with representative data, not empty databases or toy assets.
63- Close unrelated apps to reduce noise during profiling.
64- Keep measurements before and after the fix so the outcome is concrete.
65- Add `os_signpost` markers when a workflow needs ongoing timing visibility.
66
67## Xcode Diagnostics
68
69Recommend relevant Scheme > Run > Diagnostics settings when they match the suspected issue:
70
71| Setting | Use For |
72| --- | --- |
73| Main Thread Checker | UI work off the main thread |
74| Thread Sanitizer | Data races and unsafe shared state |
75| Address Sanitizer | Buffer overflows and use-after-free |
76| Malloc Stack Logging | Allocation call stacks |
77| Zombie Objects | Messages to deallocated objects |
78
79## MetricKit Hook
80
81Suggest MetricKit for production monitoring of launch, responsiveness, memory, and diagnostics:
82
83```swift
84import MetricKit
85
86final class PerformanceReporter: NSObject, MXMetricManagerSubscriber {
87 func startCollecting() {
88 MXMetricManager.shared.add(self)
89 }
90
91 func didReceive(_ payloads: [MXMetricPayload]) {
92 for payload in payloads {
93 if let launch = payload.applicationLaunchMetrics {
94 log("Resume time: \(launch.histogrammedResumeTime)")
95 }
96 if let responsiveness = payload.applicationResponsivenessMetrics {
97 log("Hang time: \(responsiveness.histogrammedApplicationHangTime)")
98 }
99 if let memory = payload.memoryMetrics {
100 log("Peak memory: \(memory.peakMemoryUsage)")
101 }
102 }
103 }
104
105 func didReceive(_ payloads: [MXDiagnosticPayload]) {
106 for payload in payloads {
107 if let hangs = payload.hangDiagnostics {
108 for hang in hangs {
109 log("Hang: \(hang.callStackTree)")
110 }
111 }
112 }
113 }
114}
115```
116
117## Review Checklist
118
119Responsiveness:
120- No synchronous work on the main thread over 100 ms.
121- No file I/O or network calls on the main thread.
122- Large Core Data or SwiftData fetches use background contexts.
123- Images decode off the main thread.
124- `@MainActor` is limited to code that truly needs UI access.
125
126Memory:
127- No retain cycles in delegates, closures, observers, or async tasks.
128- Large resources are released when no longer visible.
129- Collections and caches are bounded.
130- `autoreleasepool` is used in tight loops that create Objective-C objects.
131
132Launch:
133- No heavy work in `init()` of the `@main App` struct.
134- Non-essential initialization is deferred.
135- Dynamic frameworks are minimized where practical.
136- No synchronous network calls occur during launch.
137
138Energy:
139- Background tasks use the appropriate `BGTaskScheduler` request type.
140- Location accuracy matches the product need.
141- Timers use tolerance so the system can coalesce wakeups.
142- Network requests are batched and cached where possible.
143
144## References
145
146- `references/time-profiler.md`: CPU profiling, hang detection, signpost API.
147- `references/memory-profiling.md`: Allocations, Leaks, Memory Graph debugger.
148- `references/launch-optimization.md`: Launch phases and cold/warm start optimization.
149- `references/energy-diagnostics.md`: Battery, thermal state, and network efficiency.