ollama ps to inspect loaded memory. Then (1) unload idle background models with keep_alive: 0, (2) cap your context window (PARAMETER num_ctx 4096) to shrink the KV cache, and (3) switch from Q8/Q5 weights to a Q4_K_M model.Hitting an Ollama out of memory (or CUDA out of memory / model load failure) error occurs whenever Model Weights + KV Cache + Parallel Buffers exceed your available GPU VRAM or system RAM. Fortunately, most OOM crashes are caused by default context/cache settings rather than broken hardware.

Why Ollama Runs Out of Memory Unexpectedly
Even if a model file is only 4.5 GB on disk, three hidden factors multiply its live footprint:
- KV Cache Growth: Long context windows (32K or 128K tokens) reserve gigabytes of memory just to store attention states.
- Idle Models Stay Loaded: By default, Ollama keeps recently used models in memory (
keep_alive, usually 5 minutes) so subsequent queries start instantly. Switching between two models back-to-back can overflow RAM. - Parallel Request Buffers: Running multiple concurrent requests multiplies KV cache memory per parallel slot.
Fix 1: Inspect Active Memory with ollama ps
Run this command while Ollama is active to see every currently loaded model and how much memory it holds:
ollama psIf an older model appears in the list when you meant to run a different one, unload it immediately.
Fix 2: Unload Idle Models via CLI or API
In current Ollama versions, you can stop a loaded model directly from the terminal or via the local REST API with zero-second keep_alive:
ollama stop llama3.2:3b
curl http://localhost:11434/api/generate -d '{"model": "llama3.2:3b", "keep_alive": 0}'Fix 3: Shrink the Context Window (KV Cache) with a Modelfile
If a model crashes specifically when you paste long documents or expand context in a chat UI, cap num_ctx explicitly to 4096 or 2048 tokens:
FROM qwen2.5:7b
PARAMETER num_ctx 4096Create and run the memory-capped variant:
ollama create qwen-4k -f ./Modelfile
ollama run qwen-4kAlso limit concurrent parallel requests and flash-attention overhead when starting the server manually:
OLLAMA_NUM_PARALLEL=1 OLLAMA_MAX_LOADED_MODELS=1 ollama serveFix 4: Switch to Q4_K_M or a Smaller Parameter Tier
If you pulled a Q8_0 or Q5_K_M model on an 8 GB laptop, drop to Q4_K_M or move from 7B down to a 3B model such as llama3.2:3b or qwen2.5:3b. Compare memory numbers in our GGUF quantization guide and LLM RAM requirements table.
Frequently Asked Questions
Why did Ollama crash after I upgraded my chat UI context slider?
Raising context from 4,096 to 32,768 tokens multiplies the KV cache roughly eightfold, quickly exceeding 8 GB–16 GB RAM limits even if the model weights previously fit fine.
How do I permanently prevent Ollama from keeping two models loaded?
Set the environment variable OLLAMA_MAX_LOADED_MODELS=1 before launching ollama serve so Ollama automatically evicts the previous model whenever you switch.
Does Ollama out of memory happen on CPU-only laptops too?
Yes. When system RAM plus swap fills up completely during CPU inference, the operating system kills the Ollama runner process. Dropping to a 1.5B–3B Q4_K_M model resolves this immediately.