<!--
{
  "documentType" : "article",
  "framework" : "Xcode",
  "identifier" : "/documentation/Xcode/preventing-memory-use-regressions",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Preventing memory-use regressions"
}
-->

# Preventing memory-use regressions

Measure the memory that your app’s features use, and detect increases by using XCTest
performance tests.

## Overview

<doc://com.apple.documentation/documentation/XCTest> can measure the amount of memory
allocated by your app while it executes a test case. To measure memory use, create
a performance test in your app’s unit test target, and pass an instance of <doc://com.apple.documentation/documentation/XCTest/XCTMemoryMetric>
to `measure(metrics:)`. Inside the block, call the code from your app that demonstrates
the problematic memory use.

```swift
class MemoryTests: XCTestCase {
    func testMemoryUse() {
        self.measure(metrics: [XCTMemoryMetric()]) {
          // Use the relevant app feature here
        }
    }
}
```

When you run this test, Xcode measures the peak memory use observed while the block
runs, and the growth in memory allocated between the block’s beginning and end. You
can observe these values by clicking the icon next to the test results. Click Set
Baseline to establish a value for future comparisons. The test fails if the memory
use significantly exceeds the baseline measurement.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)