Shipping an AI app or agent? Security review in 1 working day: $149 / ₹13,900. Request a review →
AI Review Ready
Home / Error fixes / CUDA out of memory
Fix · Ollama · GPU

Ollama CUDA error: out of memory: the fix

The GPU ran out of memory while Ollama was loading or running a model. Model weights plus the context (KV cache) didn't fit in VRAM, often because the context length or the number of parallel requests is larger than the card can hold.

Fix it

  1. See what's loaded: ollama ps shows each model's size, how it is split (PROCESSOR: 100% GPU, 100% CPU or a split) and its CONTEXT.
  2. Lower the context length. More context needs more memory. Per server: OLLAMA_CONTEXT_LENGTH=8192 ollama serve. In ollama run: /set parameter num_ctx 8192. Per API request: "options": {"num_ctx": 8192}.
  3. Free the GPU: ollama stop <model> unloads a model now. Set OLLAMA_MAX_LOADED_MODELS=1 to keep only one loaded.
  4. Limit parallel requests: OLLAMA_NUM_PARALLEL=1. Parallel requests multiply the context: a 2K context with 4 parallel requests allocates an 8K context.
  5. Shrink the KV cache: OLLAMA_FLASH_ATTENTION=1 with OLLAMA_KV_CACHE_TYPE=q8_0, about half the memory of the default f16 with a very small loss of precision (q4_0 is about a quarter, with more loss).
  6. 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

CheckWhat it tells you
ollama ps CONTEXT columnA 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 modelsOther loaded models hold VRAM; by default up to 3 per GPU can stay loaded, each for 5 minutes after use
nvidia-smiOther programs (games, browsers, training jobs) using the same GPU
Server log with OLLAMA_DEBUG=1What 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 (default 5m), or "keep_alive": 0 in an API call, unloads models sooner.
  • CUDA_VISIBLE_DEVICES: pick which NVIDIA GPUs Ollama uses. UUIDs from nvidia-smi -L are more reliable than numbers; an invalid ID such as -1 forces CPU only.
  • Multiple GPUs: Ollama puts a model on one GPU if it fits there, otherwise spreads it across all of them; OLLAMA_SCHED_SPREAD always 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

Sources

Read 29 Nov 2026. These tools change often: if a step no longer matches, the linked page wins.

Related free tools