Fixing Apple Silicon Metal OOM & Swap Thrashing: Memory Compression & Heap Fixes
- Sudden speed collapses on 16GB Macs happen when
llama-server's default RAM cache and multi-slot buffers add a2,374 MBresident heap atop a7.37 GiBmodel, colliding with the12.42 GiBMetal cap. - Passing
--cache-ram 0 --no-webui --no-slots -sm none --no-repack --no-hostreduced Dirty Malloc Large by-90.1%(2,374 → 236 MB; stock best213 MB), dropped physical footprint by-68.4%(2.840 → 0.897 GB), flipped swap delta from+2,621.2to-122.8 MB, and lifted decode throughput from10.30to16.21 tok/s (+57.4%). - Conversely,
--load-mode none(footprint inflated to11.0 GB),GGML_METAL_TENSOR_ENABLE=1(prefill-17%/46.32 tok/s), andq8_0/f16KV caches degraded performance and were reverted.
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)
| 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
--load-mode none(mmap disabled): Pinned weights in anonymous memory, inflating physical footprint to11.0 GB.GGML_METAL_TENSOR_ENABLE=1: Slowed prefill throughput by -17% (46.32 tok/s) on M4.q8_0/f16KV cache: Higher memory pressure and slower generation thanq4_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.