7 min read

Why Ollama Runs Out of Memory, and How to Fix It

Ollama out of memory errors usually come from context length, parallel requests or other loaded models, not the model file. How to find which one, and fix it.

By Cogniq Labs ResearchEvidence policy

InferenceLanguage ModelsCompute
Isometric illustration of a GPU board stacked with model layers and cache blocks, with the overflow spilling onto a separate, dimmer slab of system memory and a few blocks glowing red.

When Ollama runs out of memory, the model file is rarely the whole story. Most of the time the problem is the memory Ollama sets aside next to the model: the context window, extra copies for parallel requests, and other models still loaded. Check ollama ps, lower the context length, and unload what you are not using. That fixes most cases without changing the model.

This note explains where the memory goes, how to see it, and the fixes in the order worth trying them.

First, see what is loaded

Run:

ollama ps

Ollama's documentation shows output like this:

NAME             ID              SIZE      PROCESSOR    CONTEXT    UNTIL
gemma4:latest    c6eb396dbd59    9.6 GB    100% GPU     131072     2 minutes from now

Three columns matter:

  • SIZE is the memory the loaded model takes, including its context cache. It is usually larger than the file you downloaded, and the difference is mostly context.
  • PROCESSOR shows where it runs. The FAQ explains that 100% GPU means the model is entirely on the GPU, 100% CPU means it is entirely in system memory, and a figure such as 48%/52% CPU/GPU means it is split between the two.
  • CONTEXT is the context length the model was loaded with. In the documentation's own example, that is 131,072 tokens.

If more than one model is listed, each one is taking memory.

Where the memory goes

Memory is used by four things:

  1. The model weights, roughly the size of the download.
  2. The KV cache for the context window. It grows with the context length and is set aside when the model loads, whether or not your prompts are long.
  3. Parallel requests. The FAQ says parallel processing increases the context size by the number of parallel requests, and that required memory scales with OLLAMA_NUM_PARALLEL times OLLAMA_CONTEXT_LENGTH.
  4. Other loaded models. By default Ollama keeps models in memory for 5 minutes after use, and can hold up to 3 models per GPU at once.

The second item is the one people miss. As illustrative arithmetic: a Llama 3.1 8B style model stores about 128 KiB of cache per token at 16-bit, from its configuration of 32 layers, 8 key-value heads and a head dimension of 128.

Context length KV cache, one request With 4 parallel requests
4,096 0.5 GiB 2 GiB
32,768 4 GiB 16 GiB
131,072 16 GiB 64 GiB

The 4-bit weights of that model are about 5 GB. At a long context, the cache can be several times the size of the model itself. That model supports up to about 128K tokens, which is why the table stops there. We explain the formula in how much VRAM you need for a 70B model.

If the message is about system rather than GPU memory, see model requires more system memory than is available.

A worked example: an 8 GB card

Here is how those numbers play out on a common setup, an 8B model at 4-bit on a GPU with 8 GB of memory. This is arithmetic, and the allowance for the desktop and runtime overhead is an assumption.

Item Memory
GPU memory 8 GB
Desktop, drivers and runtime overhead (assumed) about 1 GB
4-bit model weights about 5 GB
Left for the context cache about 2 GB

At a 4,096-token context, the cache needs 0.5 GiB, which fits with room to spare. At 32,768 tokens it needs 4 GiB, which does not, so Ollama either fails or moves part of the model onto the CPU. An 8-bit cache brings 32K down to 2 GiB, right at the limit. Four parallel requests at 4K also need 2 GiB.

The model itself never changed in any of these cases. Only the settings around it did, which is why lowering the context length is the first thing to try.

Why it can change without you doing anything

The context length page says Ollama now picks a default context length from your VRAM: 4K tokens below 24 GiB, 32K from 24 to 48 GiB, and 256K at 48 GiB or more. Older documentation gives smaller defaults: the FAQ says 4,096 tokens, and the Modelfile reference lists 2,048.

So an update, or moving to a bigger GPU, can quietly raise the context length and the memory reserved for it. A setup that fitted can stop fitting, or a model that ran on the GPU can start splitting onto the CPU. If something changed after an update, check the CONTEXT column first. We cover the settings in detail in Ollama context length.

The fixes, in order

1. Lower the context length

This is the biggest lever. Set it for the whole server:

OLLAMA_CONTEXT_LENGTH=8192 ollama serve

Or for a single request through the API, with num_ctx in the options object. Pick the longest context you actually use, not the largest the model supports.

2. Run one request at a time

If you set OLLAMA_NUM_PARALLEL above 1, each parallel slot needs its own share of context memory. Set it back to 1 and see whether the error goes away. If it does, you are trading concurrency for memory, and you can raise it again carefully with a shorter context.

3. Unload models you are not using

Check ollama ps for other models, and unload them:

ollama stop llama3.2

The FAQ also documents unloading through the API by sending a request with "keep_alive": 0. Setting OLLAMA_MAX_LOADED_MODELS=1 stops several models from piling up in memory. The FAQ notes that when using GPU inference, new models must be able to completely fit in VRAM to allow concurrent model loads.

4. Store the cache at 8 bits

The FAQ lists three settings for OLLAMA_KV_CACHE_TYPE. The default, f16, has high precision and high memory use. q8_0 uses about half the memory with a very small loss in precision, and q4_0 about a quarter, with a small to medium loss. Start with q8_0, and test long prompts afterwards.

5. Check what else is on the GPU

A browser, a game, another AI tool or a second Ollama instance all take VRAM that Ollama has to share. Run nvidia-smi and look for other processes. On a laptop or a desktop with a single GPU, the desktop itself can use a noticeable share.

6. Use a smaller quantization

If the weights alone barely fit, pick a smaller quantization tag of the same model, or a smaller model. We covered the quality cost in what quantization actually costs you.

When it does not crash but gets slow

Often Ollama does not fail at all. It loads what fits on the GPU and runs the rest on the CPU. (If ollama ps shows 100% CPU rather than a split, the GPU was never found; see why Ollama is using the CPU instead of your GPU.) The model works, but generation can become many times slower. ollama ps shows this as a CPU/GPU split. The fixes are the same as above: free enough GPU memory that PROCESSOR shows 100% GPU again. The context length page puts it plainly: for best performance, avoid offloading the model to the CPU.

Two related problems are worth knowing. The hardware support page says that on Linux, after a suspend and resume, Ollama sometimes fails to find the NVIDIA GPU and falls back to the CPU, and gives a driver reload as the workaround. And if you have several GPUs, CUDA_VISIBLE_DEVICES controls which ones Ollama uses.

If instead the model process stops with an error, see reading Ollama's "process has terminated" error.

Read the server log

When the cause is not obvious, the server log usually says what Ollama tried to load and where. The troubleshooting page gives the locations:

System Where to find the log
macOS ~/.ollama/logs/server.log
Linux with systemd journalctl -u ollama
Windows server.log in %LOCALAPPDATA%\Ollama
Docker docker logs <container-name>

Setting OLLAMA_DEBUG=1 adds more detail. The same page lists CUDA error codes that point to a GPU that could not be initialised rather than a shortage of memory, which needs a different fix: drivers, not settings. The hardware page lists supported NVIDIA cards and the minimum driver versions.

A quick checklist

  1. Run ollama ps. Note SIZE, PROCESSOR and CONTEXT.
  2. Set OLLAMA_CONTEXT_LENGTH to what you actually need.
  3. Set OLLAMA_NUM_PARALLEL=1 while testing.
  4. Unload other models with ollama stop.
  5. Try OLLAMA_KV_CACHE_TYPE=q8_0.
  6. Close other GPU programs.
  7. Only then, try a smaller quantization.

If you are serving many users rather than running models for yourself, the same reasoning applies to vLLM, which fails differently: see why vLLM runs out of memory.

What we do not know

We have not measured these settings on hardware; the cache figures are arithmetic from a published configuration. Ollama changes quickly, and its own pages give different defaults, so ollama ps and the server log on your machine are better guides than any article. Exactly how Ollama estimates memory before loading a model also varies between versions, so treat the order of fixes above as a sensible starting point rather than a rule.

Sources

  1. Ollama FAQ — Ollama
  2. Context length — Ollama
  3. Hardware support — Ollama
  4. Troubleshooting — Ollama

Frequently asked questions

Why does Ollama run out of memory?

Usually because of everything loaded alongside the model weights: the context window's KV cache, which Ollama reserves in full when the model loads, extra copies of it for parallel requests, and other models still held in memory. Lowering the context length and unloading idle models fixes most cases.

How do I fix "CUDA out of memory" in Ollama?

Check ollama ps to see what is loaded and at what context length. Then lower the context with OLLAMA_CONTEXT_LENGTH or num_ctx, set OLLAMA_NUM_PARALLEL to 1, unload other models with ollama stop, and close other programs using the GPU. If it still fails, use a smaller quantization of the model.

How do I see how much memory an Ollama model is using?

Run ollama ps. It lists each loaded model with its size in memory, whether it is on the GPU, the CPU or split between them, and its context length. The size includes the context cache, which is why it is larger than the model file.

Does Ollama use the GPU or the CPU?

The GPU when the model fits, and the CPU for whatever does not. The PROCESSOR column of ollama ps shows 100% GPU, 100% CPU, or a split such as 48%/52% CPU/GPU. A split model works but generates much more slowly.

Can I reduce Ollama memory use without a smaller model?

Yes. Lower the context length, run one request at a time, keep only one model loaded, and store the KV cache at 8 bits with OLLAMA_KV_CACHE_TYPE=q8_0, which Ollama's docs say uses about half the memory of the default with a very small loss in precision.

Related reading