Fix it
- See what's loaded:
ollama psshows each model's size, how it is split (PROCESSOR:100% GPU,100% CPUor a split) and itsCONTEXT. - Lower the context length. More context needs more memory. Per server:
OLLAMA_CONTEXT_LENGTH=8192 ollama serve. Inollama run:/set parameter num_ctx 8192. Per API request:"options": {"num_ctx": 8192}. - Free the GPU:
ollama stop <model>unloads a model now. SetOLLAMA_MAX_LOADED_MODELS=1to keep only one loaded. - Limit parallel requests:
OLLAMA_NUM_PARALLEL=1. Parallel requests multiply the context: a 2K context with 4 parallel requests allocates an 8K context. - Shrink the KV cache:
OLLAMA_FLASH_ATTENTION=1withOLLAMA_KV_CACHE_TYPE=q8_0, about half the memory of the defaultf16with a very small loss of precision (q4_0is about a quarter, with more loss). - Still failing? Use a smaller model or a smaller quantization tag of the same model.
Set these variables for the Ollama server and restart it (systemctl edit ollama.service on Linux, launchctl setenv on macOS, user environment variables on Windows), or they won't take effect.
What the error looks like
CUDA error: out of memory
current device: 0, in function alloc at .../ggml-cuda/ggml-cuda.cu:529
cuMemCreate(&handle, reserve_size, &prop, 0)
Ollama's own docs don't quote this text; the example is from issue #17089, where it came from a server running with OLLAMA_CONTEXT_LENGTH=65536. The same message, and cudaMalloc failed: out of memory, appear in other open GPU issues.
Causes, and how to check
| Check | What it tells you |
|---|---|
ollama ps CONTEXT column | A very large context on a small card is the usual cause. Ollama picks its default from your VRAM: 4k under 24 GiB, 32k for 24–48 GiB, 256k at 48 GiB or more. |
ollama ps lists several models | Other loaded models hold VRAM; by default up to 3 per GPU can stay loaded, each for 5 minutes after use |
nvidia-smi | Other programs (games, browsers, training jobs) using the same GPU |
Server log with OLLAMA_DEBUG=1 | What Ollama detected and planned during GPU discovery and load |
More settings that help
OLLAMA_GPU_OVERHEAD: reserve part of each GPU's VRAM (in bytes), leaving headroom for other software.OLLAMA_KEEP_ALIVE(default5m), or"keep_alive": 0in an API call, unloads models sooner.CUDA_VISIBLE_DEVICES: pick which NVIDIA GPUs Ollama uses. UUIDs fromnvidia-smi -Lare more reliable than numbers; an invalid ID such as-1forces CPU only.- Multiple GPUs: Ollama puts a model on one GPU if it fits there, otherwise spreads it across all of them;
OLLAMA_SCHED_SPREADalways spreads it.
Driver and card requirements
Ollama supports NVIDIA GPUs with compute capability 5.0 or higher and driver 550 or newer; compute capability 5.0 to 6.2 needs driver 570 or newer. On Windows the docs list NVIDIA driver 551.61 or newer. If the GPU isn't used at all after a Linux suspend/resume, reload the driver: sudo rmmod nvidia_uvm && sudo modprobe nvidia_uvm.
Ollama crashes instead of reporting the error
Issue #17089 (open) reports Ollama crashing or hanging on CUDA out-of-memory instead of returning an error. Until that changes, lowering context and parallelism (steps 2 and 4) is what keeps the load under your VRAM.
Running Claude Code, Codex or Cursor on real repos, or shipping an AI agent? A Quick Review ($149 / ₹13,900, 1 working day) checks one repo, config or setup for leaked keys, risky agent permissions and MCP servers. Request a Quick Review
FAQ
How do I reduce Ollama's GPU memory use?
Lower the context length (num_ctx or OLLAMA_CONTEXT_LENGTH), set OLLAMA_NUM_PARALLEL=1 and OLLAMA_MAX_LOADED_MODELS=1, and quantize the KV cache with OLLAMA_FLASH_ATTENTION=1 and OLLAMA_KV_CACHE_TYPE=q8_0.
How do I know if my Ollama model is on the GPU?
Run ollama ps. The PROCESSOR column shows 100% GPU, 100% CPU, or a CPU/GPU split.
What context length does Ollama use by default?
Ollama's context-length docs say it depends on VRAM: 4k under 24 GiB, 32k for 24 to 48 GiB, 256k at 48 GiB or more. Check the actual value in the CONTEXT column of ollama ps.
How do I force Ollama to run on the CPU?
Set CUDA_VISIBLE_DEVICES to an invalid GPU ID such as -1 for the server.
Related errors
- Ollama: context deadline exceeded / pull model manifest
- ollama: command not found
- Claude API error 529 overloaded_error
Sources
Read 29 Nov 2026. These tools change often: if a step no longer matches, the linked page wins.
- Ollama docs: FAQ: docs.ollama.com/faq
- Ollama docs: Context length: docs.ollama.com/context-length
- Ollama docs: Hardware support (GPU): docs.ollama.com/gpu
- ollama/ollama source: envconfig/config.go: github.com/ollama/ollama/blob/main/envconfig/config.go
- Ollama docs: Troubleshooting: docs.ollama.com/troubleshooting
- ollama/ollama issue #17089 (crash on CUDA out of memory): github.com/ollama/ollama/issues/17089
- LLM API cost checker: find LLM calls with no limits or retries capped
- MCP config checker: scan your mcp.json for hard-coded tokens, unpinned servers and risky access
- reviewready CLI: one command that checks an AI app repo before it ships