Browse Questions
  • A memory leak is memory that is allocated but never freed — usually from retain cycles.
  • A diffable data source is a data source that you drive with immutable snapshots of Hashable identifiers instead of index-path callbacks, and it computes the inserts, deletes, and moves for you.
  • UITableView reuses cell objects as they scroll off screen rather than creating new ones, keeping memory usage constant regardless of how many rows exist.
  • withCheckedContinuation bridges old callback-based APIs into the async/await world.

Answer: A memory leak is memory that is allocated but never freed — usually from retain cycles. Over time it causes the app to use more and more memory and eventually crash.

How to find leaks with Instruments:

  1. Xcode → Product → Profile (or Cmd+I)
  2. Choose Leaks template
  3. Use the app to trigger the suspected leak
  4. Instruments marks leaked objects in red
  5. Click a leak to see the allocation stack trace

Finding retain cycles with Memory Graph:

  1. Run the app in Xcode
  2. Click the Memory Graph button (3 circles icon in debug bar)
  3. Look for objects that shouldn't exist — e.g. MyViewController that was dismissed
  4. Click the object to see what's retaining it

Code Example — common leak:

// ❌ Retain cycle — both objects keep each other alive
class ViewController: UIViewController {
    var timer: Timer?

    override func viewDidLoad() {
        // Timer retains self strongly
        timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
            self.update()  // strong capture
        }
    }
    // timer never invalidated → ViewController never deallocated
}

// ✅ Fix
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
    self?.update()
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    timer?.invalidate()
}