# Speed Up CI Builds with a RAM Disk on Mac

Canonical: https://figment.so/blog/mac-ramdisk-derived-data-ci-speed

Mount a RAM disk with hdiutil, point xcodebuild at it with -derivedDataPath, and time the build against your SSD. On Figment, only the 48GB Dedicated M4 Pro has room for a useful one (up to 16GB at one-third of RAM).

**To put DerivedData on a RAM disk, create one with `hdiutil`, format it with `diskutil`, and point `xcodebuild` at it with `-derivedDataPath`. Then time the build against your SSD, because the gain depends on your project and Mac.** Keep the disk to about one-third of the Mac's RAM. On Figment hardware that means the Dedicated M4 Pro 48 GB (48GB RAM, so up to 16GB of RAM disk). The Intel Mac VM (4GB) and the Apple Silicon Mac VM (4GB) are too small for a useful RAM disk.

## When does a RAM disk speed up Xcode builds?

It helps most on write-heavy clean builds on slower storage. During a clean build Xcode writes module caches, intermediate `.o` objects, `.swiftmodule` files and index data in parallel across many compiler processes. On older Intel Macs with slower SSDs, moving DerivedData to a RAM disk can cut wall time by a double-digit percentage. On modern Apple silicon with Apple's NVMe controller the gap is smaller and depends on the project.

Xcode's compiler driver schedules many parallel jobs, and each writes to DerivedData independently. On SATA SSDs or older PCIe drives those writes queue behind disk queue-depth limits, so wall-clock time goes to waiting rather than compiling. A RAM disk moves reads and writes to DRAM latency (nanoseconds) from flash latency (microseconds). The benefit is largest on:

- Intel Macs with consumer-grade SATA or early PCIe SSDs
- Clean builds that regenerate all DerivedData from scratch
- Large projects whose DerivedData exceeds 5 GB

Incremental builds benefit less, because once most outputs exist on disk the bottleneck moves from storage to compile and link time.

## How do I create a RAM disk on macOS?

macOS has two built-in commands. `hdiutil attach -nomount ram://<sectors>` returns a block device backed by RAM, and `diskutil erasevolume` formats and mounts it. The unit is a 512-byte sector, so:

```
sectors = megabytes × 2048
```

For an 8 GB (8,192 MB) RAM disk: 8,192 × 2048 = **16,777,216 sectors**. Check: 16,777,216 × 512 = 8,589,934,592 bytes = 8 GiB.

```bash
DEVICE=$(hdiutil attach -nomount ram://16777216)
diskutil erasevolume HFS+ "XcodeDerivedData" "${DEVICE}"
# Volume mounted at /Volumes/XcodeDerivedData
```

For the `xcodebuild` flags used below, see the [Xcode build settings reference](https://developer.apple.com/documentation/xcode/build-settings-reference) for related DerivedData settings and the archived [xcodebuild tech note (TN2339)](https://developer.apple.com/library/archive/technotes/tn2339/_index.html) for its archive invocation pattern. Confirm the exact flag behavior against `man xcodebuild` on the Xcode version on your runner.

## How big should the RAM disk be?

Reserve at most about one-third of the Mac's RAM. Measure your project's DerivedData size first:

```bash
# After one normal build on the default path:
du -sh ~/Library/Developer/Xcode/DerivedData/YourApp-*/
```

Then pick a size with headroom.

| Project size | Typical clean DerivedData | Host RAM for a one-third RAM disk |
|---|---|---|
| Small (< 50 Swift files) | 1–2 GB | 8–16 GB |
| Mid-size (50–200 files) | 3–6 GB | 16–24 GB |
| Large (200+ files, many deps) | 6–14 GB | 24–48 GB (Dedicated M4 Pro 48 GB) |

If DerivedData grows past the RAM disk size mid-build, the build fails when the volume fills. Size conservatively and watch the first few runs. A large project that does not fit under the one-third rule should keep DerivedData on the SSD.

## What does mount-ramdisk.sh look like?

It creates the RAM disk, runs `xcodebuild` with DerivedData redirected, and removes the disk on exit whether the build succeeded or failed. Pass the workspace, scheme and output path as environment variables, and use your real scheme name.

```bash
#!/usr/bin/env bash
# mount-ramdisk.sh — build with Xcode DerivedData on a RAM disk
# Usage:
#   WORKSPACE=ios/YourApp.xcworkspace \
#   SCHEME=YourApp \
#   OUTPUT_DIR=./build \
#   RAM_DISK_MB=8192 \
#   bash mount-ramdisk.sh
set -euo pipefail

RAM_DISK_MB="${RAM_DISK_MB:-8192}"
DISK_NAME="XcodeDerivedData"
SECTORS=$(( RAM_DISK_MB * 2048 ))

WORKSPACE="${WORKSPACE:?Set WORKSPACE to your .xcworkspace path}"
SCHEME="${SCHEME:?Set SCHEME to your Xcode scheme name}"
OUTPUT_DIR="${OUTPUT_DIR:-./build}"
mkdir -p "${OUTPUT_DIR}"

echo "==> Creating ${RAM_DISK_MB} MB RAM disk (${SECTORS} sectors)..."
DEVICE=$(hdiutil attach -nomount "ram://${SECTORS}")
diskutil erasevolume HFS+ "${DISK_NAME}" "${DEVICE}" >/dev/null
MOUNT_POINT="/Volumes/${DISK_NAME}"
echo "==> Mounted at ${MOUNT_POINT} on ${DEVICE}"

cleanup() {
  echo "==> Detaching RAM disk ${DEVICE}..."
  hdiutil detach "${DEVICE}" -quiet -force || true
}
trap cleanup EXIT

ARCHIVE_PATH="${OUTPUT_DIR}/${SCHEME}.xcarchive"
DERIVED_DATA="${MOUNT_POINT}/DerivedData"

echo "==> xcodebuild archive"
echo "    Workspace:    ${WORKSPACE}"
echo "    Scheme:       ${SCHEME}"
echo "    DerivedData:  ${DERIVED_DATA}"
echo "    Archive path: ${ARCHIVE_PATH}"

xcodebuild \
  -workspace "${WORKSPACE}" \
  -scheme "${SCHEME}" \
  -configuration Release \
  -derivedDataPath "${DERIVED_DATA}" \
  -archivePath "${ARCHIVE_PATH}" \
  archive
```

`set -euo pipefail` makes the script exit on the first failure rather than report a failed build as a fast run. `trap cleanup EXIT` runs `hdiutil detach` on normal exit, error or signal, so a killed CI job does not leak the RAM device until reboot. `-derivedDataPath` is a command-line argument to `xcodebuild`; it does not go in an `.xcconfig` build setting file.

For prettier output, pipe through a formatter in a way that preserves the real exit status, for example `set -o pipefail; xcodebuild ... | xcbeautify`. Do not swallow the exit code of `xcodebuild` with a plain pipe.

## How do I measure the speedup?

Run the same archive twice from an identical starting state. Record the wall-clock `real` time from `time` and put both rows in a small worksheet.

```bash
# Clean baseline each run
rm -rf ~/Library/Developer/Xcode/DerivedData

# Baseline: default SSD DerivedData
time xcodebuild \
  -workspace ios/YourApp.xcworkspace \
  -scheme YourApp \
  -configuration Release \
  -archivePath ./build/ssd-test.xcarchive \
  archive

# RAM disk: via the script
rm -rf ~/Library/Developer/Xcode/DerivedData
time WORKSPACE=ios/YourApp.xcworkspace \
     SCHEME=YourApp \
     OUTPUT_DIR=./build \
     RAM_DISK_MB=8192 \
     bash mount-ramdisk.sh
```

The worksheet below uses made-up numbers for illustration. Replace them with your own.

| Run | DerivedData location | real time | Delta vs SSD |
|---|---|---|---|
| SSD #1 | `~/Library/Developer/Xcode/DerivedData` | 287 s | — |
| SSD #2 | same | 281 s | — |
| RAM #1 | `/Volumes/XcodeDerivedData/DerivedData` | 243 s | −38 s |
| RAM #2 | same | 239 s | −45 s |

If the average SSD time is 284 s and the average RAM time is 241 s, the saving is 284 − 241 = 43 s per build, or 43 ÷ 284 ≈ 15.1%. Run at least two of each, discard the first warm run if you want steady-state behavior, and draw conclusions only when the delta is bigger than the run-to-run variance. For finer disk-activity detail, use `sudo fs_usage -f diskio | grep DerivedData` or Instruments' disk templates.

## Does a RAM disk help on Apple silicon?

Less than on an older Intel Mac. Apple silicon Macs share one unified memory pool across CPU and GPU and have fast internal NVMe, so the ratio between DRAM speed and SSD speed is much tighter than on a 2015-era Intel Mac with a SATA SSD. The advantage shrinks and depends on the project. Run the worksheet on your own project before you build a RAM disk into production CI. If the delta is small, spend the time on other levers: explicit module builds, splitting slow test targets, and caching dependencies between runs. The guide on [multiple Xcode versions in CI](/blog/mac-multiple-xcode-versions-ci) covers related setup.

## Which Figment Mac plan fits a RAM disk?

Use the [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro) ($349 a month, or $299 a month on a 12-month term): it is a whole Mac with an M4 Pro chip, 48GB RAM and a 512GB SSD, so a 16GB RAM disk leaves 32GB for Xcode and the compilers. It is billed by the month and you can cancel any time, so run the worksheet in the first days. The [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) has 4GB RAM, which leaves no useful RAM disk. The [Intel Mac VM](/mac-hosting#intel-vm) has 4GB RAM and runs macOS Sequoia. Check the macOS version shown before purchase against Apple's Xcode table.

**Related guides:** [Multiple Xcode versions in CI](/blog/mac-multiple-xcode-versions-ci) · [Upload dSYMs in CI for crash reporting](/blog/mac-dsym-upload-ci-crash-reporting) · [Remote Xcode setup on Windows](/blog/mac-xcode-on-windows-remote-setup)
