Smart Systems for Home

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

Fix Ollama Out of Memory & Slow Inference Errors

· 6 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

If you're running Ollama locally and hitting "out of memory" errors or painfully slow inference, the cause is almost always a mismatch between the model's size, your system's RAM/VRAM, and Ollama's default settings. This guide walks you through practical fixes—from simple restarts to adjusting environment variables and choosing the right model—so you can get back to generating responses without crashes or long waits.

What You'll Need

  • Access to a terminal (Command Prompt, PowerShell, or bash)
  • Admin/sudo rights if you need to change system environment variables
  • Basic knowledge of your system's hardware (RAM, GPU VRAM, CPU cores)
  • Ollama installed (any recent version)

Step-by-Step

  1. Check your current model size and system resources. Run ollama list to see installed models and their sizes. Then check your available RAM (Windows: Task Manager > Performance; macOS: Activity Monitor; Linux: free -h) and GPU VRAM (nvidia-smi for NVIDIA, or task manager for Windows). This tells you how much headroom you have.
  2. Restart the Ollama service. Sometimes a stale process holds onto memory. On Linux/macOS: sudo systemctl restart ollama (or brew services restart ollama on macOS). On Windows: right-click the Ollama tray icon and select "Quit", then start it again. This clears any memory leaks from long-running sessions.
  3. Reduce the number of parallel requests. If you're using Ollama in a script or with a UI like Open WebUI, limit concurrent generations. Ollama processes requests sequentially by default, but if you've set OLLAMA_NUM_PARALLEL to a high number, lower it to 1 or 2. Set it via environment variable: OLLAMA_NUM_PARALLEL=1 (see step 5 for how to set env vars).
  4. Lower the context window size. A large context (e.g., 8192 tokens) consumes significant memory. When running a model, use the OLLAMA_CONTEXT_LENGTH environment variable or the num_ctx parameter in your API call. For example, to set 2048: OLLAMA_CONTEXT_LENGTH=2048. This is often the quickest win for both memory and speed.
  5. Set environment variables to control memory usage. Ollama respects several env vars. The most important are:
    • OLLAMA_MAX_LOADED_MODELS: number of models kept in memory simultaneously (default 1). Set to 1 to avoid swapping.
    • OLLAMA_KEEP_ALIVE: how long a model stays loaded after a request (default 5m). Set to a shorter time like 30s to free memory sooner.
    • OLLAMA_NUM_PARALLEL: as above.
    To set these permanently on Windows: go to System Properties > Environment Variables > New. On macOS/Linux: add export OLLAMA_MAX_LOADED_MODELS=1 to your ~/.bashrc or ~/.zshrc and restart the shell.
  6. Use a smaller model or a quantized version. If you're using a 7B model and running out of memory, try a 3B model like llama3.2:3b or a quantized version (e.g., q4_K_M instead of q8_0). Pull a smaller model with ollama pull llama3.2:3b. Quantization reduces memory usage with minimal quality loss.
  7. Force GPU offloading (if you have a GPU). Ollama automatically uses GPU if available, but you can control how many layers are offloaded with OLLAMA_GPU_LAYERS. Set it to a high number like 99 to offload as much as possible to VRAM. On Windows, ensure you have the latest NVIDIA drivers and CUDA toolkit installed.
  8. Increase swap/virtual memory (as a last resort). If you're still OOM, you can increase system swap. On Linux: sudo fallocate -l 8G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile. On Windows: adjust virtual memory in System Properties > Advanced > Performance Settings > Advanced > Virtual Memory. This prevents crashes but may slow inference due to disk I/O.
  9. Update Ollama. Run ollama update (or download the latest version) to get memory management improvements and bug fixes.

Common Mistakes

  • Ignoring the difference between RAM and VRAM. Ollama uses VRAM first for GPU offloading, but if the model doesn't fit entirely in VRAM, it spills to system RAM, which is much slower. This causes slow inference, not necessarily OOM.
  • Setting OLLAMA_KEEP_ALIVE to -1. This keeps models loaded forever, which can exhaust memory if you switch between models. Use a finite time.
  • Not restarting Ollama after changing environment variables. Env vars are read at startup. If you change them, you must restart the service for changes to take effect.
  • Assuming all models are the same size. A 13B model uses roughly twice the memory of a 7B model. Check the actual size in ollama list.
  • Using a huge context window unnecessarily. If you're only asking short questions, a context of 2048 is usually enough. Bigger isn't always better.

Tips

  • Monitor memory usage in real-time. Use nvidia-smi (GPU) and top/htop (RAM) while running a request to see where the bottleneck is.
  • Use the /set parameter num_ctx in interactive mode. If you're using ollama run, you can set the context length per session without changing global env vars.
  • Prefer quantized models. Models like llama3.2:3b-q4_K_M are much lighter and often fast enough for most tasks.
  • Batch requests. If you have many prompts, send them in one request with a larger context instead of many separate calls, to reduce overhead.
  • Keep your GPU drivers updated. Outdated drivers can cause Ollama to fall back to CPU, which is dramatically slower.

Troubleshooting

  • Still getting "out of memory" after following steps? Check the Ollama logs. On Linux/macOS: journalctl -u ollama or ~/.ollama/logs/server.log. On Windows: %LOCALAPPDATA%\Ollama\server.log. Look for lines mentioning "memory" or "failed to allocate".
  • Inference is still slow but no OOM. This usually means the model is running on CPU. Check if GPU offloading is active: run ollama run llama3.2 and look for "offload" messages in the log. If not, set OLLAMA_GPU_LAYERS=99 and restart.
  • Changes to env vars don't seem to take effect. Ensure you're setting them in the correct scope. On Windows, user vs system variables matter. On Linux, if you're using systemd, you may need to edit the service file (systemctl edit ollama) to add environment variables.
  • Model loads but crashes mid-generation. This could be a context overflow. Reduce the context length further or use a smaller model.
  • Ollama won't start after changing settings. Reset to defaults by unsetting the env vars and restarting. If that fails, reinstall Ollama.

Frequently Asked Questions

What does "out of memory" mean in Ollama?

It means the model plus its context window require more RAM or VRAM than your system has available. The model needs memory for weights, activations, and the key-value cache for the context.

How much RAM do I need for a 7B model?

Roughly 4-6 GB for a quantized 7B model (q4), and 8-10 GB for a full 7B model. Plus additional memory for the context window (about 1 GB per 2048 tokens).

Can I run Ollama on CPU only?

Yes, but it will be much slower. Set OLLAMA_GPU_LAYERS=0 to force CPU. Expect inference times of minutes per response for larger models.

Why is my GPU not being used?

Check that you have a supported GPU (NVIDIA with CUDA, or Apple Silicon). Ensure drivers are installed. Also, Ollama may not use GPU if the model is too large to fit in VRAM—it then falls back to CPU.

What is the best context length for speed?

For most tasks, 2048 tokens is a good balance. Larger contexts increase memory usage and slow down inference because the model has to process more tokens.

Does Ollama support multiple GPUs?

Yes, Ollama can split a model across multiple GPUs. Set CUDA_VISIBLE_DEVICES to list them, and Ollama will use all available VRAM.