Find and investigate iOS app hangs with MetricKit
Capture Apple's hang diagnostics in Sentry, then inspect the representative stack trace and flame graph to investigate work that leaves your app unresponsive.
Before you start
App and SDK
- An iOS app with the Sentry Apple SDK installed and initialized
- Sentry Apple SDK 9.31.0 or later and a device running iOS 15 or later
Accounts and access
- A Sentry account with access to your iOS project's issues and DSN
- Access to the app's build pipeline and matching dSYM files
Knowledge
- Basic familiarity with Swift, Xcode, and the app flow you want to investigate
1 Enable MetricKit in your existing SDK setup
Add enableMetricKit = true to your existing SentrySDK.start configuration. Replace the DSN placeholder with your project's DSN and keep your other SDK options. Do not start the SDK a second time. This subscribes the SDK to hang, CPU, and disk write diagnostics. You do not need to enable tracing or profiling to receive MetricKit reports.
Existing Sentry initializationimport Sentry
SentrySDK.start { options in
options.dsn = "___PUBLIC_DSN___"
options.enableMetricKit = true
}
2 Upload debug symbols for the build you will investigate
Native addresses are much more useful when Sentry can resolve them to function names. Use your existing Xcode build phase, Fastlane, or CLI upload workflow to send the dSYMs that match the app build. In project settings, select your iOS project and open Debug Files to check the upload. Symbols from a different build do not resolve the captured addresses.
- Generate release debug information using DWARF with dSYM File.
- Include dSYMs for your app and any of your frameworks involved in the hang.
- Keep symbol uploads in the release pipeline so later hang reports have matching files.
3 Enable flame graph attachments
Set options.experimental.metrickit.hangReportingMode = .flamegraphAttachment in the same startup configuration. The SDK sends the sampled call tree as a flamegraph.json attachment, separate from the event stack trace so ordinary stack trace limits do not trim the graph. Sentry symbolicates the attachment using the same debug symbols as error events. This mode is available for public testing in SDK version 9.31.0 and later, and the configuration option is experimental.
- The hang-only setting shown above requires SDK version 9.32.0 or later. On SDK version 9.31.x, omit
options.experimental.metrickit.enabledDiagnosticReports = [.hang]to capture hang, CPU, and disk write diagnostics. - Attachments consume attachment quota, and the associated hang events consume error-event quota. No profiling quota is required.
- Without the attachment setting, the SDK uses legacy reporting, which stores the sampled tree in the event's stack trace.
MetricKit with flame graph attachmentsimport Sentry
SentrySDK.start { options in
options.dsn = "___PUBLIC_DSN___"
options.enableMetricKit = true
options.experimental.metrickit.hangReportingMode = .flamegraphAttachment
// Requires Sentry 9.32.0
options.experimental.metrickit.enabledDiagnosticReports = [.hang]
}
4 Run the app and wait for a system diagnostic
Build and run the configured app on a supported device without a debugger attached, then exercise the flow that users report as unresponsive. MetricKit collects diagnostics through the operating system, not through a manual capture call. Apple decides which hangs to report and how much detail to include, so reproducing a delay does not guarantee a diagnostic. The setup estimate above does not include this waiting time.
- MetricKit does not report hang diagnostics while a debugger is attached. Launch the app directly on the device or detach Xcode's debugger before reproducing the hang.
- Use a test build or an existing affected flow. Do not add a deliberate main-thread freeze to production code.
- Do not tune
appHangTimeoutIntervalto make MetricKit report sooner. That option belongs to the separate App Hang tracking integration. - If no report appears, check the deployed SDK configuration, event filtering, and project quota before assuming the app had no hangs.
5 Find a hang event and inspect its stack trace
Open Issues, select your iOS project, and search for error.mechanism:mx_hang_diagnostic in the relevant environment and time range. Open a matching issue and inspect an event. Confirm the exception mechanism is mx_hang_diagnostic, then read the hang duration, app build, and representative stack trace.
- Sentry selects a representative stack trace for normal issue grouping using sample counts, stack depth, and in-app frames.
- Treat the selected stack as a starting point for investigation, not proof of the hang's cause.
- Grouping is best-effort. A hang's duration affects the samples available to calculate its representative stack trace, so hangs with the same cause can produce different stacks and appear in separate issues. Compare events across releases and related issues.
6 Read the sampled call paths to form a hypothesis
Open the event's Flamegraph section and select a call tree. The graph includes sampled branches outside the representative stack trace. Inspect the wider application paths and the system calls beneath them. In the sample app below, checkout work branches into product decoding, receipt encoding, and price calculations. A wide decoding path suggests checking whether large payloads are being parsed on the main thread.
- Width represents sample count, including samples in descendants. It does not directly measure a function's duration.
- Horizontal position does not represent execution order. These are aggregate observations, not timestamped profiling samples.
- Some trees aggregate observations across threads. Do not assume every tree is a main-thread stack.
- Missing graphs can reflect attachment quota, retention, or access restrictions. Check these alongside the event's attachments.
7 Test a targeted fix and compare later reports
Use the sampled paths to identify a specific operation to investigate, such as synchronous I/O, repeated parsing, or lock contention. Reproduce the flow and use local debugging or performance tools to check your hypothesis. Make a targeted change, upload the new build's symbols, and repeat the same flow. Compare later reports by release and call path, alongside direct responsiveness testing. The absence of a new MetricKit report alone does not prove the hang is fixed.
- Record the affected build, reported duration, and suspected application path before changing the code.
- Check that the change improves responsiveness without changing the operation's intended behavior.
- Look across related issues rather than relying on one issue's grouping or event count.
That's it.
You have a starting point for the hang.
System-provided diagnostics show sampled call paths to investigate when your app stops responding to input.
- Enabled MetricKit hang diagnostics with flame graph attachments
- Prepared matching debug symbols to resolve native addresses
- Found hang events and the representative stack traces used for issue grouping
- Used sampled call paths to investigate a hang without mistaking them for an execution timeline
Pro tips
- 💡 Start with the widest relevant application paths, then inspect the calls beneath them. A common system function can be a symptom of work initiated by your code.
- 💡 Keep matching dSYMs for every shipped build and upload them as part of your release pipeline.
- 💡 For payload troubleshooting, set
options.enableMetricKitRawPayload = trueto attach the original diagnostic as JSON. It is separate fromflamegraph.jsonand adds attachment usage. - 💡 Use the reported duration as context for the hang, not as a way to turn every frame's sample count into exact elapsed time.
Common pitfalls
- ⚠️ Enabling MetricKit does not guarantee a report for every unresponsive interaction. Apple controls diagnostic collection and delivery.
- ⚠️ Disabling or pausing App Hang tracking does not stop MetricKit reports. They are separate integrations.
- ⚠️ The representative stack trace is a heuristic. Check the broader flame graph and reproduce the affected flow before deciding which operation to change.
- ⚠️ Enabling MetricKit without setting
hangReportingModekeeps legacy reporting. If the SDK cannot create a flame graph attachment, it also falls back to legacy reporting.
Frequently asked questions
options.enableMetricKitRawPayload = true to the same startup configuration. The SDK attaches the original diagnostic as JSON alongside flamegraph.json. The raw payload is useful for troubleshooting but is not required for the flame graph viewer.What's next?
Fix it, don't observe it.
Get started with the only application monitoring platform that empowers developers to fix application problems without compromising on velocity.