Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix Out of Memory Errors When Running a Local LLM

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Running a large language model (LLM) on your own computer can hit a wall when you run out of RAM or VRAM. This guide walks through practical steps to reduce memory usage and get your model running smoothly, from adjusting quantization to tweaking system settings.

What You'll Need

  • A computer with a dedicated GPU (NVIDIA, AMD, or Intel) with at least 4 GB VRAM, or a CPU with 16 GB RAM (for smaller models).
  • An LLM runtime installed: Ollama, LM Studio, llama.cpp, or similar.
  • A model file downloaded (e.g., Llama 3, Mistral, Phi).
  • Basic comfort with command line if using llama.cpp.

Step-by-Step

  1. Identify the bottleneck. Open Task Manager (Windows) or Activity Monitor (macOS/Linux) and watch memory usage while loading the model. If GPU memory (VRAM) is full, the error likely says "CUDA out of memory." If system RAM is full, you'll see a general "Out of memory" or the process crashes.
  2. Choose a smaller quantized version. Quantization reduces model size by using fewer bits per weight. Common levels: Q4_K_M, Q5_K_M, Q8_0. For example, a 7B model in FP16 takes ~14 GB, but Q4_K_M takes ~4 GB. Download a Q4 or Q5 version from Hugging Face or use the runtime's built-in lists.
  3. Unload other memory-heavy apps. Close browsers with many tabs, video editors, or other large programs. On Windows, check for background processes like OneDrive or antivirus scanning.
  4. Adjust context length. The context window (how many tokens the model remembers) consumes memory. Reduce it to 2048 or 1024 tokens. In Ollama, set num_ctx; in LM Studio, use the slider; in llama.cpp, use -c 2048.
  5. Enable GPU offloading (if using llama.cpp). Use -ngl (number of GPU layers) to offload as many layers as possible to VRAM. Start with -ngl 20 and increase until you hit memory limits. If you have a GPU, this speeds up inference.
  6. Use CPU offloading with memory mapping. If you have more RAM than VRAM, let the model partially reside in system RAM. llama.cpp supports --mmap (default) which maps the model file to memory, reducing RAM usage.
  7. Increase swap/virtual memory. On Windows: System Properties → Advanced → Performance Settings → Advanced → Virtual memory → set a custom size (e.g., 1.5x your RAM). On Linux, add a swap file. This won't make it fast, but it prevents crashes.
  8. Use a model that fits your hardware. If a 13B model won't fit, try a 7B or 3B model. For CPU-only, a 7B Q4 model needs ~6 GB RAM, so 16 GB RAM is comfortable.
  9. Update GPU drivers. Outdated drivers can cause memory allocation failures. Go to NVIDIA/AMD/Intel's website and install the latest driver.
  10. Restart the runtime. Sometimes memory isn't freed properly. Close the app completely and reopen.

Common Mistakes

  • Ignoring VRAM vs RAM. A model that fits in RAM may not fit in VRAM. Check both.
  • Using too high a context length. Even with a small model, a 32k context can eat 8+ GB. Start small.
  • Not using quantization. Downloading the full FP16 model when a Q4 would work is the #1 cause of OOM.
  • Forgetting to close other GPU apps. Chrome, Discord, and even some games use VRAM. Close them.
  • Setting swap too small. If you set virtual memory to a tiny fixed size, the system can't expand when needed.

Tips

  • Use LM Studio or Ollama—they auto-select quantization and offloading for your hardware.
  • For CPU-only, use llama.cpp with -t to set thread count (e.g., -t 8 for 8 cores).
  • Monitor memory in real-time with nvidia-smi (GPU) and htop (RAM).
  • Consider using a GGUF format model—it's designed for efficient memory mapping.
  • If you have multiple GPUs, enable multi-GPU support (e.g., --split in llama.cpp).

Troubleshooting

Still OOM after reducing context and quantization? Try an even smaller model (e.g., Phi-3 mini instead of Llama-3 8B).

CUDA out of memory but VRAM seems free? Other processes may be reserving memory. Check with nvidia-smi and kill unused processes.

Model loads but crashes during generation? This often means the context grew and exceeded memory. Reduce context further or use a smaller model.

Slow but no error? The model is swapping to disk. Increase swap or reduce model size.

Error "Cannot allocate memory" on Linux? Check ulimit -v (virtual memory limit) and increase it with ulimit -v unlimited.

Frequently Asked Questions

Q: Can I run a 70B model on 16 GB RAM?
A: Only with heavy quantization (Q2) and very low context, but it will be extremely slow. Better to use a 7B or 13B model.

Q: What does "quantization" mean?
A: It reduces the precision of the model's weights (e.g., from 16-bit to 4-bit), shrinking memory usage by ~75% with minimal quality loss.

Q: Should I use GPU or CPU for inference?
A: GPU is faster, but CPU can handle larger models if you have enough RAM. Use GPU offloading when possible.

Q: How do I know how much memory a model needs?
A: Rough rule: for FP16, multiply parameters by 2 bytes (7B → 14 GB). For Q4, multiply by ~0.6 (7B → ~4.2 GB). Add context memory (~1-2 GB).

Q: Why does my model use more RAM than the file size?
A: The file is compressed; when loaded, it expands into memory. Also, context and compute graphs add overhead.

Q: Can I use external GPU (eGPU) to avoid OOM?
A: Yes, but eGPU bandwidth is limited. It helps if your internal GPU is low on VRAM.