Sub topic 17
Troubleshooting memory, speed, and connection problems
This chapter assumes you can use a terminal, have installed Ollama, and have run a model with ollama run. You should also know the basics of the REST API and the idea of a context window from earlier chapters. Here you will learn a repeatable way to diagnose the three most common failure types: the server can't be reached, the model runs out of memory, and responses are slow.
A diagnostic order that saves time
Most problems fall into one of three layers. Check them in this order, because a failure at a lower layer makes the layers above it meaningless.
- Connection: Is the server running, and is your client talking to the right address?
- Model and memory: Did the model load, and where does it live (GPU, CPU, or both)?
- Speed: The model loads and answers, but it is slower than expected.
Connection problems
Check whether the server answers
The Ollama server listens on port 11434. A plain request to the root path is the quickest test.
curl http://localhost:11434A healthy server replies with the text Ollama is running. You can also list models through the API:
curl http://localhost:11434/api/tags"Connection refused" or "could not connect to server"
This means nothing is listening at the address you used. Work through these causes:
- The server is not running. Start it with
ollama servein a terminal, or start the desktop app or system service you installed earlier. If you installed a service on Linux,systemctl status ollamashows whether it is active, andjournalctl -u ollamashows its log. - Port already in use. If another process holds port 11434, the server fails to start. The log names the bind error. Stop the other process, or set
OLLAMA_HOSTto a different port. - Wrong address in the client. A script that uses
http://localhost:11434/v1works for the OpenAI-compatible endpoint, buthttp://localhost:11434is the base for the native API. Mixing the two gives 404 errors, not refused connections. - Client and server on different machines or containers. Inside a Docker container,
localhostmeans the container itself. Use the host's address or a service name instead.
Reaching the server from another machine
By default the server binds to 127.0.0.1, so only the local machine can connect. To accept remote connections, set OLLAMA_HOST before starting the server.
OLLAMA_HOST=0.0.0.0:11434 ollama serveThen open the port in the host's firewall. Ollama has no built-in authentication, so anyone who can reach the port can use your models. Put a reverse proxy with authentication in front of it, or restrict access to a trusted network.
The same variable also tells the CLI which server to talk to. If you run ollama list in a shell where OLLAMA_HOST points to a remote machine, you see that machine's models.
A model name that does not exist
An error such as model "llama3.3" not found is not a connection problem. The server answered, but the name is not in your local store. Run ollama list to see the exact names and tags, then either use one of those or run ollama pull first. Remember that llama3.2 without a tag means llama3.2:latest.
Memory problems
What uses memory
A loaded model needs memory for three things:
- Weights: the model file itself. Its size depends on parameter count and quantization.
- KV cache: the stored attention state for the conversation. It grows with
num_ctx, the context window size. - Overhead: buffers and runtime structures, usually smaller than the other two.
A common mistake is to compare only the download size to your free memory. A model with a 4 GB download can need noticeably more than 4 GB once loaded with a large context window.
Symptoms and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Error mentioning insufficient memory when loading | Model plus context does not fit in available RAM or VRAM | Choose a smaller model or a lower quantization such as Q4, and reduce `num_ctx` |
| Model loads but is very slow | Part of the model is on the CPU | Check `ollama ps`, then shrink the model or context until it fits on the GPU |
| System freezes or swaps heavily | Model fills RAM and the operating system uses disk | Close other applications, or use a smaller model |
| Process killed without a message (Linux) | The kernel's out-of-memory killer ended it | Check `dmesg` or `journalctl` for an OOM entry, then reduce memory use |
| Earlier model disappears | Another model was loaded and the first was unloaded to make room | Expected behavior. Raise concurrency limits only if you have the memory |
Use `ollama ps` to see where the model is
ollama ps lists models in memory and shows a PROCESSOR column. The value tells you where the model runs.
ollama ps100% GPUmeans the whole model is in video memory. This is the fast case.100% CPUmeans no GPU is used. Generation works but is much slower.- A split such as
40%/60% CPU/GPUmeans the model did not fit in video memory, so layers are shared. Speed falls well below the all-GPU case because data crosses between the two memories.
The same column also shows the size in memory, which is usually larger than the download. Compare that figure to your free VRAM.
Reduce the context window
The context window is a frequent hidden cost. If a model is just barely too large, lowering num_ctx can bring it fully onto the GPU. You can set it per request through the API options:
import requests
resp = requests.post(
"http://localhost:11434/api/chat",
json={
"model": "llama3.2",
"messages": [{"role": "user", "content": "Summarize what a KV cache is."}],
"options": {"num_ctx": 2048},
"stream": False,
},
timeout=120,
)
resp.raise_for_status()
print(resp.json()["message"]["content"])Changing num_ctx for a model that is already loaded makes the server reload it with the new setting. The cost is a short delay on that request. A smaller window also means the model forgets older parts of long conversations or documents, so reduce it only as far as your task allows.
Speed problems
Separate load time from generation time
A slow first answer and slow later answers have different causes.
- First request is slow. The model must be read from disk into memory. A large model on a slow disk can take many seconds. Later requests are fast because the model stays loaded.
- Every request reloads the model. Ollama unloads an idle model after a keep-alive period, which is five minutes by default. If your requests are spaced further apart than that, each one pays the load cost again. Set
OLLAMA_KEEP_ALIVEfor the server, or passkeep_alivein a request, to hold the model longer. - Generation itself is slow. Look at where the model runs, as above. CPU-only or split execution is the most common cause.
The response from a non-streaming request includes timing fields, and these show which phase is slow.
import requests
resp = requests.post(
"http://localhost:11434/api/generate",
json={"model": "llama3.2", "prompt": "Name three prime numbers.", "stream": False},
timeout=120,
)
resp.raise_for_status()
data = resp.json()
load_s = data["load_duration"] / 1e9
eval_s = data["eval_duration"] / 1e9
tokens = data["eval_count"]
print(f"load: {load_s:.2f}s")
print(f"generation: {tokens} tokens in {eval_s:.2f}s ({tokens / eval_s:.1f} tokens/s)")The durations are reported in nanoseconds, so divide by 1e9 for seconds. A large load_duration points at loading or keep-alive. A low tokens-per-second figure points at placement on the CPU or a model that is too large for the hardware.
You can get similar numbers from the command line with ollama run llama3.2 --verbose, which prints timing statistics after each reply.
Other causes of slowness
- Long prompts. The model must process the entire prompt before it produces the first token, and that work grows with prompt length. A long conversation history makes every turn slower.
- Parallel requests. Several simultaneous requests share the same hardware. Each one slows down, and extra requests may queue.
- Large model on modest hardware. A model that fits in memory can still be slow if it is big. A smaller model or a more aggressive quantization trades some quality for speed.
- GPU not detected. If
ollama psshows100% CPUon a machine with a GPU, check the server log at startup. Out-of-date drivers or an unsupported GPU are typical reasons. Update the driver, restart the server, and look for a line that names the detected device.
Reading the server log
When the cause is unclear, the log usually says. Its location depends on the platform.
| Platform | Where to look |
|---|---|
| Linux with systemd | `journalctl -u ollama` |
| macOS | `~/.ollama/logs/server.log` |
| Windows | `%LOCALAPPDATA%\Ollama\server.log` |
| Docker | `docker logs <container>` |
Look for the GPU detection lines near startup, memory estimates when a model loads, and any error text. Quote those lines when you ask others for help, because they identify the cause far faster than a description like "it's slow."
A short checklist
curl http://localhost:11434to confirm the server responds.ollama listto confirm the model name and tag.ollama psto see size and processor split.- Lower
num_ctxor choose a smaller quantization if the model is not fully on the GPU. - Check
load_durationand tokens per second to decide between a loading problem and a generation problem. - Read the server log for GPU detection and error lines.
Exercise
You run ollama ps and see a PROCESSOR value of 35%/65% CPU/GPU. Generation is much slower than a friend's results on a similar model. What does the value mean, and what are two changes you could try?
Show answer
The model did not fit entirely in video memory, so about 35% of it runs on the CPU and 65% on the GPU, and data moving between the two slows generation. You could lower num_ctx to shrink the KV cache, or choose a smaller model or a lower quantization such as Q4 so the whole model fits on the GPU. Closing other programs that use video memory can also help.
Exercise
A script calls your model once every ten minutes. The first reply after each gap is slow, but replies within a conversation are fast. Which response field would confirm the cause, and what setting would fix it?
Show answer
The load_duration field would be large on the slow requests, showing that the model is being reloaded. The default keep-alive is five minutes, so the model is unloaded between calls. Setting OLLAMA_KEEP_ALIVE on the server, or passing keep_alive in each request, to a value longer than ten minutes keeps the model in memory.