refusal rate
Subscribe

Ollama out of memory? Load failures, mid-reply CUDA crashes and 'requires more system memory' need different fixes. Fix them in this exact order.

[ fig. 01 — troubleshooting ]48×5
[ fig. 01 — troubleshooting ]16×5

Short answer

An Ollama out-of-memory error is one of three problems. 'unable to allocate CUDA0 buffer' or 'cudaMalloc failed' at load means the weights, KV cache and compute buffer don't fit in VRAM. A crash seconds into a reply means a buffer grew after loading. 'model requires more system memory' means free RAM, not VRAM, ran out. Fix VRAM errors by shrinking the quant, num_ctx and parallel slots first, then flash attention, KV quantization and num_gpu.

Default context (source)
4k under 24 GiB VRAM, 32k at 24 to 48 GiB, 256k at 48+
Memory scales with
OLLAMA_NUM_PARALLEL x context length
KV cache types
f16 (default), q8_0 (~1/2), q4_0 (~1/4); needs flash attention
Headroom setting
OLLAMA_GPU_OVERHEAD, in bytes per GPU

Key takeaways

  1. Read the error before touching settings. Load-time VRAM failures, mid-generation crashes and the 'requires more system memory' refusal have different causes and different first fixes.
  2. Weights are rarely the whole story. In one GitHub report the allocation that failed was a 4.9 GiB compute buffer, reserved after 27.8 GiB of weights already fit.
  3. Context and parallel slots multiply. Ollama's FAQ says required memory scales with OLLAMA_NUM_PARALLEL times the context length, and the default context jumps to 32k on 24 to 48 GiB cards.
  4. num_gpu is the manual override. Lowering the number of offloaded layers fixed load failures in two GitHub reports, at the cost of speed.
  5. 'Requires more system memory' is a RAM check. Unload idle models with ollama stop, and in Docker check how much memory the container can actually see.

“Ollama out of memory” is not one error. It is three, and the fix for one can do nothing for another. Before you change a setting, find out which one you have: the message in the client, plus the server log, tells you whether VRAM ran out while loading, VRAM ran out while generating, or system RAM was too short for Ollama to even try.

Everything below comes from Ollama’s documentation, its source code, or GitHub issues where the logs show the cause. Where a fix is a community report rather than documented behavior, it says so.

What does each Ollama out-of-memory error mean?

Each message points at a different memory pool and a different moment, so read it before anything else. Ollama’s troubleshooting page lists where the server log lives: ~/.ollama/logs/server.log on macOS, journalctl -u ollama on Linux with systemd, docker logs <container> in Docker, and %LOCALAPPDATA%\Ollama\server.log on Windows.

Error textWhen it happensMemory poolTypical cause
unable to allocate CUDA0 buffer / unable to allocate ROCm0 bufferWhile loadingVRAMWeights, KV cache and compute buffer together exceed the card
cudaMalloc failed: out of memoryUsually while loadingVRAMSame, often on the last allocation, the compute buffer
CUDA error: out of memory, then error reading llm response in the clientSeconds into a replyVRAMA buffer grew after load; little headroom left
model requires more system memory (X GiB) than is available (Y GiB)Before loadingSystem RAMFree RAM is short: other models, apps, page cache, or a container limit
ollama ps shows a split such as 48%/52% CPU/GPUAfter loadingBothNot an error: the model spilled to RAM and runs slowly

The last row is the quiet version of the problem. If the model loads but ollama ps shows a CPU/GPU split, you are not crashing, you are just slow; that diagnosis lives in Ollama not using GPU.

[ fig. 01 — Which levers to pull for each Ollama out-of-memory error, cheapest first. Sources: Ollama FAQ; maintainer list in ollama#8597 ]
Triage diagram: three Ollama out-of-memory errors, each with its levers in order. Load failures: smaller quant, lower num_ctx, OLLAMA_NUM_PARALLEL=1, flash attention with q8_0 KV cache, lower num_gpu, OLLAMA_GPU_OVERHEAD. Mid-reply crashes: lower num_ctx, flash attention, OLLAMA_GPU_OVERHEAD, lower num_gpu. The system-memory refusal: unload idle models and free RAM.

Why does Ollama run out of memory when the model file fits?

Because the GPU holds three things, and the model file is only one of them. Ollama allocates the weights, then the KV cache for the whole context window, then a compute buffer for the forward pass. The KV cache grows linearly with context, and the compute buffer is allocated last, so it is often the allocation that fails.

Issue #11202 shows the arithmetic clearly. A user with two 32 GB AMD MI50s loaded a 671B model with 8 of 62 layers on the GPUs. The log shows 28,426.68 MiB of weights and 1,280 MiB of KV cache on the first card, then a failed attempt to reserve 5,030 MiB more for the compute graph: cudaMalloc failed: out of memory. Ollama had asked one card to hold about 33.9 GiB.

[ fig. 02 — Memory requested on GPU0 at load, from the log in ollama#11202 (MiB converted to GiB) ]
Stacked bar: on one 32 GB MI50, Ollama tried to place 27.8 GiB of weights, 1.25 GiB of KV cache and a 4.9 GiB compute buffer, 33.9 GiB in total, so the compute buffer allocation failed; limiting num_gpu to 6 layers let the model load

Since September 2025, Ollama’s new engine measures memory before loading instead of estimating it. The announcement says this “significantly reduced crashes due to out of memory issues” and that ollama ps now matches nvidia-smi. At launch it applied to models on the new engine, such as gpt-oss, gemma3 and qwen3, with llama3.x and qwen3-coder listed as “coming soon.” If an older model family still crashes, it may still be on the estimate path, and updating Ollama is a reasonable first step.

Why does memory run out after the model loaded fine?

Usually because the context or the number of parallel slots is larger than you think, or because the card had no slack for allocations made during generation.

Two settings multiply each other. Ollama’s FAQ says “required RAM will scale by OLLAMA_NUM_PARALLEL * OLLAMA_CONTEXT_LENGTH”, and its example is a 2k context with 4 parallel requests becoming 8k. OLLAMA_NUM_PARALLEL defaults to 1 in the current source, so check whether your service file or a tutorial set it higher.

The default context is also not fixed. The context length docs set it by VRAM: 4k under 24 GiB, 32k from 24 to 48 GiB, and 256k at 48 GiB or more. Ollama’s own pages disagree with each other here: the FAQ still says 4096 and the Modelfile reference says 2048. The source code matches the VRAM-based rule. Clients add their own value on top: in a Continue issue about Llama 3.1 8B, the maintainer’s fix for an Ollama memory error was to lower Continue’s contextLength, because Continue’s default can be higher than other tools’. How each client sets it is covered in Ollama context length.

Mid-reply crashes also come from having no headroom at all. In issue #6382, a 70B model loaded on a 16 GB Tesla P100 with 29 of 81 layers offloaded, then crashed within seconds of each prompt. A maintainer suggested fewer GPU layers and flash attention; the reporter’s other model, gemma2, ran once num_gpu was lowered.

Which settings fix Ollama out of memory errors, in what order?

Start with the levers that cost nothing in speed, and finish with the ones that trade speed for fit. An Ollama maintainer posted a mitigation list in issue #8597; this order rearranges it by cost and adds the two levers that come before any setting.

  1. Use a smaller quant or a smaller model. A Q4_K_M instead of Q8_0 roughly halves the weights. It is the only lever that shrinks the biggest block. Sizing by GPU is in local LLM hardware requirements.
  2. Lower the context. Set num_ctx per request or OLLAMA_CONTEXT_LENGTH on the server. Going from 32k to 8k cuts the KV cache by three quarters.
  3. Set OLLAMA_NUM_PARALLEL=1. Each extra slot reserves its own context; how the slots work is in OLLAMA_NUM_PARALLEL.
  4. Turn on flash attention. The FAQ says Ollama enables it automatically where the backend supports it, and OLLAMA_FLASH_ATTENTION=1 forces it. It “can significantly reduce memory usage as the context size grows.”
  5. Quantize the KV cache. OLLAMA_KV_CACHE_TYPE=q8_0 uses about half the memory of the f16 default with a “very small” precision loss; q4_0 uses about a quarter. It only applies with flash attention on, and it is global for all models. Trade-offs are in KV cache quantization.
  6. Reserve headroom with OLLAMA_GPU_OVERHEAD. The source describes it as “Reserve a portion of VRAM per GPU (bytes)”. The maintainer’s example is 536870912 for 512 MiB. Ollama then plans around a smaller card, which leaves room for buffers that grow.
  7. Offload fewer layers with num_gpu. This works every time, but the layers that move to the CPU run at RAM speed.
  8. Unload other models. ollama stop <model> frees a model now. OLLAMA_MAX_LOADED_MODELS caps how many stay loaded; the FAQ gives a default of 3 per GPU.

On Linux with NVIDIA cards the same maintainer list also mentions GGML_CUDA_ENABLE_UNIFIED_MEMORY=1, which lets CUDA spill into system memory instead of failing. Expect it to be slow.

The server-wide settings go in the environment of ollama serve:

bash
OLLAMA_NUM_PARALLEL=1 \
OLLAMA_FLASH_ATTENTION=1 \
OLLAMA_KV_CACHE_TYPE=q8_0 \
OLLAMA_CONTEXT_LENGTH=8192 \
OLLAMA_GPU_OVERHEAD=536870912 \
ollama serve

On a systemd install, put the same lines in systemctl edit ollama as Environment= entries and restart the service.

How do you control GPU layers with num_gpu?

num_gpu is the number of model layers Ollama offloads to the GPU, and setting it overrides Ollama’s own placement. In the API types it defaults to -1, meaning “set dynamically.” The Modelfile reference doesn’t document it, but a maintainer in issue #6382 gave three ways to set it:

bash
# interactive session
ollama run qwen3:32b
>>> /set parameter num_gpu 40

# per request
curl http://localhost:11434/api/generate -d '{"model":"qwen3:32b","prompt":"hi","options":{"num_gpu":40}}'

# baked into a model
printf 'FROM qwen3:32b\nPARAMETER num_gpu 40\n' > Modelfile
ollama create qwen3-32b-40gpu -f Modelfile

To pick a number, read the load line in the server log, which says something like offloaded 8/62 layers to GPU, and start a few layers below it. The MI50 user in #11202 went from 8 to 6 layers and the model loaded at 78% and 87% use on the two cards. Each layer you remove makes generation slower, so stop as soon as it loads with a little slack.

num_gpu is not a fix for a GPU that isn’t detected. If ollama ps says 100% CPU, that is a discovery problem, covered in Ollama not using GPU.

What if you are out of system RAM rather than VRAM?

Then the message is model requires more system memory (X GiB) than is available (Y GiB), and the fixes are about RAM. Ollama compares what the model needs against free memory before it starts the runner, and returns an HTTP 500 if it doesn’t fit.

  • Unload what is already loaded. Models stay in memory for 5 minutes after use by default (FAQ). Run ollama ps, then ollama stop anything idle, or send keep_alive: 0 with requests you don’t need cached.
  • Check what “available” means on your box. In issue #13556, llama3.3 (a 39.6 GiB Q4_K_M file) ran once in Docker and then failed with “requires more system memory (39.4 GiB) than is available (30.0 GiB)”. A contributor traced it to how Ollama counts used memory inside containers, linked a fix on a personal fork, and the issue was still open in February 2026. Lowering the context didn’t help that reporter.
  • Swap is not free memory you want. An Ollama maintainer noted in #6382 that Ollama counts swap in its memory check, which is how a 39 GB model on 16 GB of VRAM and 23 GB of RAM ends up thrashing instead of failing cleanly.
  • Unified memory machines blur the line. On an NVIDIA Jetson Thor with 122.8 GiB shared memory, a forum user running gpt-oss:120b in the official container got “requires more system memory (61.4 GiB) than is available (36.4 GiB)” with no process accounting for the used memory. NVIDIA suggested a cache-cleaning script, which helped only partly.

What do developers report in practice?

The pattern across GitHub reports is that the estimate, not the hardware, is often what fails, and that num_gpu is the fix that consistently works.

  • Big model, two cards, compute buffer overflow. The MI50 report above is the clearest log: weights fit, the 4.9 GiB compute buffer did not, and PARAMETER num_gpu 6 fixed it (#11202).
  • Context as the trigger. On a 12 GB RTX 4070, one user saw CUDA error: out of memory intermittently at num_ctx 2048 and every time at 4096 or higher, on a 2024 Ollama release (#3460).
  • ROCm is less predictable. An 8 GB Radeon user hit unable to allocate ROCm0 buffer with deepseek-r1:14b after upgrading from 0.6.5 to 0.7.0. Lowering num_gpu to 9 and raising OLLAMA_GPU_OVERHEAD did not help, while llama.cpp alone used nearly the full 8 GB. The maintainer wrote that “it’s not clear to me why the OOM occurred” (#10784). If you are on AMD and the levers don’t move anything, try an older or newer Ollama release and post a debug log.
  • Containers miscount RAM. The llama3.3 Docker report above failed only on its second run (#13556).
  • Enterprise hardware isn’t immune. The maintainer’s mitigation list in #8597 was written for a user with four 46 GB L40S cards and 384 GB of RAM loading DeepSeek-R1.

Treat these as community reports on specific versions. Ollama’s memory code has changed several times since, so the version you run matters as much as the setting.

FAQ

How do I fix “CUDA out of memory” in Ollama?

Lower the context (num_ctx or OLLAMA_CONTEXT_LENGTH), set OLLAMA_NUM_PARALLEL=1, and use a smaller quant. If it still fails, enable flash attention with a q8_0 KV cache, reserve headroom with OLLAMA_GPU_OVERHEAD, then reduce num_gpu.

What does “cudaMalloc failed: out of memory” mean in Ollama?

A CUDA or ROCm allocation for the model failed because VRAM was full. In logged cases it is often the compute buffer, allocated after the weights and KV cache, so a model whose file fits can still fail. Lower the context or offload fewer layers.

What does “model requires more system memory than is available” mean?

Ollama checked free system RAM before loading and found too little. Unload idle models with ollama stop, close memory-heavy apps, lower the context, and in Docker check the container’s memory limit.

Does Ollama use system RAM when VRAM runs out?

Yes, if it plans for it: Ollama puts fewer layers on the GPU and runs the rest on the CPU, which ollama ps shows as a CPU/GPU split. A crash happens when its plan underestimates what the GPU needs, which is why num_gpu and OLLAMA_GPU_OVERHEAD exist.

Does flash attention reduce Ollama memory use?

Yes. The FAQ says it can significantly reduce memory use as context grows, and it is required before OLLAMA_KV_CACHE_TYPE takes effect.

Sources (10)
  1. FAQ: parallel requests, flash attention, K/V cache quantization, keep_alive, Ollama documentation
  2. Context length, Ollama documentation
  3. Troubleshooting: server logs and debug variables, Ollama documentation
  4. Modelfile reference, Ollama documentation
  5. envconfig/config.go: OLLAMA_GPU_OVERHEAD, OLLAMA_NUM_PARALLEL, OLLAMA_CONTEXT_LENGTH, ollama/ollama
  6. New model scheduling (exact memory measurement), Ollama blog, September 2025
  7. Unable to allocate CUDA0 buffer, maintainer's OOM mitigation list, ollama/ollama issue #8597
  8. Ollama overcommiting GPU memory with large models, ollama/ollama issue #11202
  9. cuda error out of memory, ollama/ollama issue #6382
  10. model requires more system memory on subsequent runs, ollama/ollama issue #13556

Keep reading

[ fig. 02 — explainer ]18×8

What is KV cache quantization, and should you turn it on?

Nov 2, 2026 · Explainer

KV cache quantization stores the attention keys and values a model keeps for every token at lower precision, usually q8_0 or q4_0 instead of f16. q8_0 halves the cache and, in published llama.cpp and Ollama measurements, costs almost nothing in quality. q4_0 cuts it to about 28% but is model-dependent and riskier at long context. Turn on q8_0 when context, not weights, is what runs you out of VRAM.

Read
[ fig. 03 — how-to ]18×8

How do you set the context length in Ollama, LM Studio and llama.cpp?

Nov 2, 2026 · How-to

In Ollama, set the context length with the OLLAMA_CONTEXT_LENGTH environment variable, the app's slider, or a num_ctx parameter in a Modelfile or API request. In LM Studio, set it in the model's load settings or with lms load --context-length. In llama.cpp, pass -c. Ollama defaults to 4k tokens on GPUs under 24 GiB of VRAM, and its docs recommend at least 64k for coding agents.

Read
[ fig. 04 — troubleshooting ]18×8

Why is Ollama not using your GPU, and how do you fix it?

Nov 2, 2026 · Troubleshooting

When Ollama is not using your GPU, run ollama ps while the model is loaded. A split such as 48%/52% CPU/GPU means Ollama found the GPU but the model plus its context doesn't fit in VRAM. 100% CPU means GPU discovery failed, and the server log names the reason: a driver or kernel module problem, a container without GPU access, an unsupported AMD target, or the wrong GPU selected.

Read