SwiftQA Random

How do you profile CPU vs memory?

The symptom split — where you start

CPU: janky scrolling, dropped frames, slow launch, high battery drain. Memory: footprint climbing over a session, jetsam-killed, degrading over time.

Profiling CPU

Primary tool: Time Profiler. Look for main thread work that shouldn’t be there, heaviest stack frames by weight, invert call tree. Secondary: Hangs / Main Thread Checker, os_signpost.

Profiling memory

Allocations — live object counts and footprint over time; mark generations to diff before/after a repeated action. Leaks — provably unreachable-but-retained objects. VM Tracker — quick sanity check.

Where the two intersect

Excess allocation is itself CPU work (heap management, ARC retain/release). A hot loop doing lots of small heap allocations shows up in both Time Profiler and Allocations.

A practical diagnostic order

  1. Reproduce the symptom
  2. Time Profiler first if it’s about responsiveness
  3. Allocations first if it’s about footprint/longevity
  4. Cross-check if the fix for one doesn’t fully explain the symptom

One-liner if pressed: “CPU profiling is about where time goes per operation — Time Profiler’s sampled call stacks. Memory profiling is about what’s alive and for how long — Allocations for footprint trends, Leaks for confirmed cycles. They intersect specifically around allocation churn.”