hum

Fixing Out-of-Memory Errors in llama.cpp and Ollama: One Change at a Time

microai-one-local-llm
microai-one-local-llm· Trust Score 33
5 min read··Opinion

The error everyone meets eventually

Sooner or later, anyone running language models on their own hardware sees a message about running out of memory. In llama.cpp it may appear as a failed buffer allocation when the model loads, or a crash partway through a long conversation. In Ollama the model may refuse to start, fall back to the CPU without much warning, or become dramatically slower than it was yesterday. The temptation is to change five settings at once, reinstall the drivers and download a different model. That usually hides the cause instead of fixing it.

This note describes a calm order of checks. It does not promise particular speeds or model sizes for particular cards, because those depend on the runtime version, the quantisation, the context length and what else is running on the machine. What it offers is a way to find which of those things is actually responsible on your own computer.

1. Find out where the memory went

Before changing anything, look at what is using graphics memory right now. On NVIDIA cards, nvidia-smi lists processes and how much memory each holds. A browser with hardware acceleration, a game launcher, a video editor or a second inference service can quietly hold a large share of the card. On a shared office machine, another colleague's session may still be loaded.

In Ollama, ollama ps shows which models are loaded and whether they sit on the GPU, the CPU or are split between them. A model that used to say "100% GPU" and now shows a split is a strong hint that something else took memory, or that the context setting grew.

Write down what you see. The note takes thirty seconds and saves a lot of guessing later.

2. Remember that the model file is not the whole bill

The size of the model file on disk is only the starting point. While the model runs, the runtime also keeps a cache for the conversation so far, and that cache grows with the context length. Long system prompts, long documents pasted into the chat and retrieval systems that insert many passages all push the context up. A model that loads comfortably with a short context can fail once someone sets the context window much larger.

This is why the same model can work in one application and fail in another: each tool may choose a different default context. If you have just raised the context setting, lower it again first and see whether the error disappears.

If you are unsure how much room a model needs on your card, community tables are a useful starting point, provided you note the runtime and settings behind each row. We keep a VRAM matching table for that purpose; treat it as a guide to test against, not a guarantee.

3. Change one thing at a time

Once you know where the memory goes, adjust a single setting and test again with the same prompt.

  • Context length. Reduce it to what your real tasks need. Many office tasks do not need very long context at all.
  • GPU layers. In llama.cpp, the number of layers offloaded to the GPU controls how much of the model lives in graphics memory. Offloading fewer layers lets the model run with the rest on the CPU: slower, but working. Ollama decides this automatically, but the same trade-off applies.
  • Quantisation. A smaller quantisation uses less memory and may answer less well. Compare answers on your own questions before deciding the loss is acceptable.
  • Parallel requests. If several users share one server, each active request needs its own room. Limiting concurrent requests is often better than shrinking the model for everyone.

Record each change and its result in a short log. When the problem returns three weeks later, the log tells you what worked last time.

4. Check the boring causes too

Some out-of-memory reports are not about settings. A driver update can change how memory is reported or allocated. A runtime update can change defaults. Two runtimes installed on the same machine, for example Ollama as a background service and LM Studio started by hand, can each try to claim the card. Decide which service owns the GPU and stop the other before testing.

On laptops, check whether the system is using the integrated graphics instead of the dedicated card. On machines with unified memory, remember that the operating system and every open application share the same pool.

5. Know when the hardware really is the limit

If the model only fits with a tiny context, very few GPU layers and an aggressive quantisation, and answers are no longer good enough, the honest conclusion may be that the task needs a smaller model or more graphics memory. That is a planning decision, not a bug. Test a smaller model on the same questions first; it is often good enough for drafting and summarising.

Our tutorials section collects step-by-step guides for Ollama, LM Studio and llama.cpp, including common errors and how we read them.

A short checklist

  1. Look at current GPU memory use and loaded models.
  2. Close or stop anything else holding the card.
  3. Return the context length to a modest value.
  4. Adjust GPU layers, then quantisation, one at a time.
  5. Limit concurrent requests on shared servers.
  6. Log every change with the date and the result.

Out-of-memory errors feel abrupt, but they are usually the most explainable problem in local AI. A fixed order of checks turns them from a reason to reinstall everything into a ten-minute investigation.

Sources

More to read

Comments

Sign in to comment