Disclosure: All measurements in this troubleshooting guide were captured deterministically on our own Apple M4 Mac mini (16GB) / macOS 27.2.1. See our Japanese Home or Privacy Policy.

Fixing Apple Silicon Metal OOM & Swap Thrashing: Memory Compression & Heap Fixes

Key Findings (3 Takeaways)
  • Sudden speed collapses on 16GB Macs happen when llama-server's default RAM cache and multi-slot buffers add a 2,374 MB resident heap atop a 7.37 GiB model, colliding with the 12.42 GiB Metal cap.
  • Passing --cache-ram 0 --no-webui --no-slots -sm none --no-repack --no-host reduced Dirty Malloc Large by -90.1% (2,374 → 236 MB; stock best 213 MB), dropped physical footprint by -68.4% (2.840 → 0.897 GB), flipped swap delta from +2,621.2 to -122.8 MB, and lifted decode throughput from 10.30 to 16.21 tok/s (+57.4%).
  • Conversely, --load-mode none (footprint inflated to 11.0 GB), GGML_METAL_TENSOR_ENABLE=1 (prefill -17% / 46.32 tok/s), and q8_0/f16 KV caches degraded performance and were reverted.
Decode Recovery
16.21 tok/s
10.30 → 16.21 tok/s (+57.4%)
Physical Footprint
0.897 GB
2.840 → 0.897 GB (-68.4%)
Dirty Malloc Large
236 MB
2,374 → 236 MB (-90.1%)
Inference Swap Delta
-122.8 MB
Reversed from +2,621.2 MB

1. Why the 12.42 GiB Metal Cap + Heap Bloat Triggers Swap Thrashing

The Apple M4 10-core GPU (MTLGPUFamilyApple9) exposes a Metal recommendedMaxWorkingSetSize of 12.42 GiB. Because CPU and GPU share one 16GB physical memory pool, loading a 7.37 GiB checkpoint (Ternary-Bonsai-2.5-27B-Q2_0-mtp.gguf) plus KV cache already brings GPU working memory to ~8–9 GiB. Adding llama-server's default 8,192 MiB RAM prompt cache, WebUI assets, and multi-slot buffers pushes total pressure past 12.42 GiB—forcing continuous macOS compression and swap paging (+2,621.2 MB).

Figure 1: Dirty Malloc Large Heap & Physical Footprint Reduction (16GB M4 Verified)

Physical Footprint (Lower = Safer Unified Memory Margin) Before (Default Flags) 2.840 GB (Swap +2,621.2 MB) After (M4 Tuned) 0.897 GB (-68.4%) | Swap -122.8 MB Dirty Malloc Large Heap (Lower = Eliminates Cache Bloat) Before (RAM Cache On) 2,374 MB After (--cache-ram 0) 236 MB (-90.1%) / Stock Best 213 MB (-91.0%)
Configuration (Deterministic, 4-Run Median) Medium Decode Physical Footprint Dirty Malloc Large Swap Delta
Before: Default (final_baseline) 10.30 tok/s 2.840 GB 2,374 MB +2,621.2 MB
After 1: Stock Binary Best (final_best_stock) 15.53 tok/s (+50.8%) 1.437 GB (-49.4%) 213 MB (-91.0%) +115.3 MB
After 2: M4 Custom Best (final_best_m4_custom) 16.21 tok/s (+57.4%) 0.897 GB (-68.4%) 236 MB (-90.1%) -122.8 MB

2. 30-Second Diagnostics: Metal OOM vs. Swap Thrashing

Measure your actual Metal cap, process footprint, and live vm_stat pageout activity before changing flags. Check free internal SSD space too—keep at least 50 GB free or offload checkpoints using our External NVMe Storage Guide.

# 1. Check actual Metal working-set limit (recommendedMaxWorkingSetSize)
swift -e 'import Metal; if let d = MTLCreateSystemDefaultDevice() { print(d.name, String(format: "%.2f GiB", Double(d.recommendedMaxWorkingSetSize)/1073741824.0)) }'

# 2. Inspect process physical footprint and live Swap delta
PID=$(pgrep -f "llama-server")
/usr/bin/footprint -p "$PID" | head -40
sysctl vm.swapusage
vm_stat 1 5 | grep -Ei "pageout|pagein|compress|decompress"

3. Verified Flags & Launch Configuration

Flag Role Verified Impact
--cache-ram 0 Disables default 8,192 MiB RAM prompt cache Eliminates >2 GB Dirty Malloc Large bloat
--no-webui --no-slots -sm none Removes WebUI, multi-slot, multi-GPU overhead Locks clean single-slot runtime (-np 1)
--no-repack --no-host Skips CPU weight repack & host staging buffers Startup time dropped from 3.86s to 2.08–2.33s
-ctk q4_0 -ctv q4_0 4-bit quantized KV cache Fits -c 12288 cleanly inside 12.42 GiB
--spec-type draft-mtp --spec-draft-n-max 4 --spec-draft-p-min 0.3 Enables built-in MTP speculative decoding 65.0% acceptance rate, 1.0 JSON parity, +57.4% decode
# Verified llama-server flags eliminating RAM cache bloat & swap thrashing
caffeinate -s llama-server \
  -m /path/to/Ternary-Bonsai-2.5-27B-Q2_0-mtp.gguf \
  --offline --alias bonsai-2.5-27b --no-mmproj --jinja \
  -ngl 99 -fa on -c 12288 -np 1 -ctk q4_0 -ctv q4_0 \
  --spec-type draft-mtp --spec-draft-n-max 4 --spec-draft-p-min 0.3 \
  --no-repack --no-host --no-webui --no-slots --cache-ram 0 -sm none \
  --host 127.0.0.1 --port 8080

4. Traps That Regressed in Benchmarks

Three Settings Tested and Reverted on 16GB M4
  • --load-mode none (mmap disabled): Pinned weights in anonymous memory, inflating physical footprint to 11.0 GB.
  • GGML_METAL_TENSOR_ENABLE=1: Slowed prefill throughput by -17% (46.32 tok/s) on M4.
  • q8_0 / f16 KV cache: Higher memory pressure and slower generation than q4_0.

5. Frequently Asked Questions (FAQ)

Why does a 16GB Mac report a Metal limit of 12.42 GiB instead of 16 GB?

Metal recommendedMaxWorkingSetSize measures the maximum GPU working set macOS can safely allocate after reserving space for the kernel, compressor, and unified-memory host processes. Treating 16 GB as free GPU headroom causes inference allocations to collide with the 12.42 GiB ceiling and trigger swap thrashing.

How can I distinguish Metal OOM from Swap Thrashing in 30 seconds?

If vm_stat 1 shows continuous Pageouts during token generation while throughput collapses, you have Swap Thrashing caused by excess host heap pressure. If Metal logs an immediate buffer allocation failure and exits, weights plus KV cache physically exceed the 12.42 GiB limit (Metal OOM).

Does setting --cache-ram 0 hurt prompt caching more than it helps?

On 16GB Apple Silicon, keeping the default 8,192 MiB RAM cache active inflated Dirty Malloc Large to 2,374 MB, triggered +2,621.2 MB of swap growth, and dragged decode down to 10.30 tok/s. Disabling it cut Dirty Malloc Large by 90.1% (to 236 MB), flipped swap delta to -122.8 MB, and restored 16.21 tok/s (+57.4%).

Does --load-mode none (disabling mmap) prevent paging and improve throughput?

No. In our 16GB verification run, --load-mode none pinned weights in anonymous memory and pushed physical footprint up to 11.0 GB, starving macOS of compression margin. We reverted it in favor of standard mmap.

Where can I calculate safe context sizes for my own Mac tier (8GB to 64GB+)?

Use our interactive Apple Silicon LLM Memory & Cost Calculator to model weight size, q4_0 vs f16 KV cache budgets, and 12.42 GiB Metal headroom before launching.

SiliconBench Topic Cluster

Explore Calculators & Core Benchmark Guides

Apple Silicon LLM RAM & Power Calculator →

Check unified-memory headroom and USD power costs

VPS vs Local Mac mini TCO Calculator →

Compare 3-year break-even points in USD

Speed (tok/s) & KV Cache Simulator →

Estimate bandwidth tok/s and 4k–128k KV cache scaling

Mac mini 24/7 Hardware & UPS Gear Guide →

USB4 40Gbps NVMe storage, smart meter, sine-wave UPS

M4 Mac mini 16GB Hybrid LLM Guide →

16.21 tok/s on 27B Q2_0 and 236 MB heap baseline

150-Round Thermal Endurance Benchmark →

0 thermal throttling alerts across 1K/4K/8K prompts