New Tool Radar

Local AI

How to Change How Long an Ollama Model Stays Loaded in Memory

Choose how long an Ollama model stays loaded in memory with keep_alive, so a model you use every few minutes stays ready and one you are finished with is unloaded.

Schematic diagram comparing the default 5-minute keep_alive with a longer keep_alive of 10m, 24h, or -1, and unloading now with keep_alive 0 or ollama stop llama3.2.

Closing a chat does not unload the model. Ollama’s FAQ says that by default models are kept in memory for 5 minutes before being unloaded, so a second question asked within those five minutes starts at once. That default is a compromise: it is handy when you ask questions in bursts, and wasteful when you are finished for the day. The setting that controls it is keep_alive, and you can change it for one request or for the whole server.

What keep_alive accepts

Ollama’s FAQ says the keep_alive parameter can be set with the /api/generate and /api/chat endpoints, and it lists four forms.

keep_alive value What happens When to use it
“10m” The model stays loaded for 10 minutes after the last request Questions in bursts over a morning
“24h” The model stays loaded for 24 hours A model you return to all day
3600 A number in seconds; 3600 is one hour When a tool you use sends seconds, not minutes
-1 Any negative number keeps the model loaded in memory A model that must always be ready
0 Unloads the model immediately after generating a response When you are finished and want the memory back now

The same FAQ says you can also change the time for all models by setting the OLLAMA_KEEP_ALIVE environment variable when starting the Ollama server, using the same kinds of values. A keep_alive value sent with one API request overrides the OLLAMA_KEEP_ALIVE setting for that request.

Try it: keep a model for an hour, then let it go

Send a chat request for a fictional practice model, llama3.2, with keep_alive set to “1h”, and ask the README’s sample question, “Why is the sky blue?”. Then run ollama ps after a few minutes: the model should still be listed as loaded, because its hour has not run out. When you are finished, send a request with keep_alive set to 0, or use ollama stop llama3.2 to unload it at once, and check ollama ps again to confirm it is gone. Expected result. We checked the Ollama FAQ. We did not run Ollama or send a keep_alive request on this machine.

Limits

Keeping a model loaded keeps its memory used, so a long keep_alive on a large model can leave too little memory for a second model to load — Ollama’s FAQ notes that when there is not enough memory for a new model, new requests are queued until a loaded model is unloaded. A negative keep_alive never unloads the model on its own, so it is the wrong choice on a shared or small computer. This setting changes how long a model stays loaded; it does not change how long a chat’s history is kept. We did not measure memory use ourselves.

Sources

See all