HardwareHardware problems and fixes

Ollama out of memory: why the model does not fit and what to change

When a model does not fit, Ollama either fails to load it or loads part of it into system memory and runs it slowly on the CPU. The memory goes to three things: the model's weights, the context (the KV cache), and every extra request or model held at the same time. Ollama documents a setting for each. Its troubleshooting page has no out-of-memory section, so the useful facts are spread across its FAQ and context docs; here they are in one place.

In short: Run ollama ps first: anything other than 100% GPU means the model spilled into system memory. Then cut, in this order: parallel requests and loaded models, the context length, the KV cache precision (q8_0 halves it), and finally the model size. If the agent needs a 64K context and a 7B model does not fit with it, the machine is too small for that job.

At a glance
Checkollama ps, PROCESSOR column: 100% GPU, 100% CPU or a split
Default context4k under 24 GiB VRAM, 32k at 24 to 48 GiB, 256k at 48 GiB or more
Agents needat least 64000 tokens of context, per Ollama's docs
Parallel requestsmultiply the context: 2K x 4 requests = 8K
KV cache q8_0 / q4_0about 1/2 / 1/4 of f16's memory; needs Flash Attention

See where the model landed

ollama ps

Ollama's FAQ explains the PROCESSOR column: 100% GPU "means the model was loaded entirely into the GPU", 100% CPU that it "was loaded entirely in system memory", and a split such as 48%/52% CPU/GPU that it was loaded partly onto both. A split is the quiet form of running out of memory: nothing crashes, the answers just get slow. Newer versions also show a CONTEXT column, which tells you what context the model was loaded with.

The server log says why. Ollama gives these locations:

# macOS
cat ~/.ollama/logs/server.log
# Linux (systemd)
journalctl -u ollama --no-pager --follow --pager-end

In Docker, the troubleshooting page also describes a different problem with the same symptom: Ollama "switches to running on CPU after some period of time with errors in the server log reporting GPU discovery failures". That is the GPU going missing, not memory running out; the log tells them apart.

Context length: the setting that grows quietly

Ollama's context docs: "Setting a larger context length will increase the amount of memory required to run a model." The default now depends on the memory of the GPU:

VRAMDefault context
under 24 GiB4k
24 to 48 GiB32k
48 GiB or more256k

The same page says "Tasks which require large context like web search, agents, and coding tools should be set to at least 64000 tokens." That is the tension for an agent host: the context an agent needs is many times the default on a small GPU, and raising it is often what pushes the model out of memory. Set it for the server, or per model:

OLLAMA_CONTEXT_LENGTH=64000 ollama serve

# per model, in ollama run
/set parameter num_ctx 4096
# per request, through the API
"options": {"num_ctx": 4096}

Lower it until ollama ps shows 100% GPU, and check that the agent still works at that size: cutting the context below what the agent sends trades a memory error for an agent that cannot see its whole conversation.

Parallel requests and loaded models

OLLAMA_NUM_PARALLEL is "the maximum number of parallel requests each model will process at the same time", and each one needs its own context. Ollama's example: "a 2K context with 4 parallel requests will result in an 8K context and additional memory allocation." An agent that runs sub-agents or tools in parallel against one model hits this. For a single-user agent host, OLLAMA_NUM_PARALLEL=1 is the setting that frees the most.

OLLAMA_MAX_LOADED_MODELS defaults to "3 * the number of GPUs or 3 for CPU inference". If your agent uses one model for chat and another for embeddings or summaries, both may stay resident; models stay in memory for 5 minutes after use by default. Setting OLLAMA_MAX_LOADED_MODELS=1 trades reload time for memory.

A smaller KV cache: q8_0, and Flash Attention

OLLAMA_KV_CACHE_TYPE sets the precision of the context cache, for every model the server runs. From the FAQ: f16 is the default, with "high precision and memory usage"; q8_0 "uses approximately 1/2 the memory of f16 with a very small loss in precision"; q4_0 "uses approximately 1/4 the memory of f16 with a small-medium loss in precision". It requires Flash Attention, which Ollama "uses automatically when the selected backend and devices support it"; OLLAMA_FLASH_ATTENTION=1 forces it on.

OLLAMA_FLASH_ATTENTION=1 OLLAMA_KV_CACHE_TYPE=q8_0 ollama serve

q8_0 is the boring choice: half the cache memory, a very small loss. NVIDIA's own OpenClaw tutorial for the 8 GB Jetson Orin Nano uses exactly these two settings (see the Jetson Orin Nano page). On a systemd install, put them in the service's environment rather than on a command line.

When none of that is enough: a smaller model, or more memory

The weights are the floor. These are the download sizes of common default tags on Ollama's library, at 4-bit (Q4_K_M) unless noted; the memory in use is larger once the context is added:

Model (tag)Download
llama3.2:1b (Q8_0)1.3 GB
llama3.2:3b2.0 GB
gemma3:4b3.4 GB
mistral:7b4.4 GB
llama3.1:8b4.9 GB
qwen3:8b5.2 GB
gemma3:12b8.2 GB
qwen3:14b9.3 GB
gpt-oss:20b (MXFP4)14 GB
gemma3:27b17 GB
qwen3:32b20 GB
llama3.3:70b43 GB

Going from an 8B to a 4B model, by these sizes, frees about 1.5 GB of weights before the context is counted. On a Mac, the GPU does not get all of the unified memory: community reports in llama.cpp's discussions put the default share at about two thirds to three quarters. Apple and Ollama do not document it, so treat it as an estimate, and leave room on a 16 GB machine.

If the model you need does not fit at the context the agent needs, the fix is hardware. For a 7B or 8B model with a long context next to an agent, 24 GB of unified memory is the size we point to (Mac mini M4, 16 GB vs 24 GB, Mac mini M4 on Amazon); larger models are on the Ryzen AI Max page and our estimates per agent on how much RAM an agent needs.

What to buy

Shop links go through /go/ and open Amazon (amazon.fr in the EU and Switzerland, amazon.com elsewhere). As an Amazon Associate we earn from qualifying purchases. Your price does not change and no brand pays for a place on this page (disclosure). We write no price down: it moves, the shop shows today's.

Questions

How do I know if Ollama is running out of memory?
Run ollama ps. If the PROCESSOR column shows 100% CPU or a CPU/GPU split instead of 100% GPU, the model did not fit in the GPU's memory. The server log (~/.ollama/logs/server.log on macOS, journalctl -u ollama on Linux) gives the reason.
Does a bigger context use more memory in Ollama?
Yes. Ollama's docs say a larger context length increases the memory required. Parallel requests multiply it: a 2K context with 4 parallel requests becomes an 8K context.
What does OLLAMA_KV_CACHE_TYPE=q8_0 do?
It stores the context cache at 8-bit, which Ollama says uses about half the memory of the default f16 with a very small loss in precision. It needs Flash Attention and applies to every model on the server.
What context length should I give an agent?
Ollama's docs recommend at least 64000 tokens for agents, web search and coding tools. Under 24 GiB of VRAM the default is only 4k, so set OLLAMA_CONTEXT_LENGTH yourself and check the model still fits.
Does Ollama document an out-of-memory fix?
Not as such: its troubleshooting page covers GPU discovery and logs, not memory. The settings on this page come from its FAQ and its context-length page.

Sources