New Tool Radar

Local AI

How to Lock the Model in RAM with llama-server –mlock

Use llama-server --mlock to stop the operating system swapping model weights to disk.

Schematic illustration with three step boxes: Check swap behaviour; Start with the flag; Check responsiveness.

When a system is short on memory, the operating system may swap parts of a loaded model to disk, and the next prompt then waits for the disk. The –mlock flag locks the model weights in RAM so they cannot be swapped out. This guide starts the server with the flag and checks that the first prompt after idle time stays fast.

Before you start

  • llama.cpp installed, with llama-server on your PATH.
  • A GGUF model file, for example a Qwen3 8B Q4_K_M file.
  • Enough RAM to hold the whole model; locking only works if the model fits.

See whether swapping happens now

Load the model, leave the server idle for ten minutes, then send a prompt and note the time to the first token. If the first token is much slower after idle time than right after loading, swapping is a likely cause.

Start the server with the flag

Start the server with memory locking on:

llama-server -m qwen3-8b-q4_k_m.gguf --mlock --port 8080

On Linux, the process may need a raised memory-lock limit; if it fails with a lock error, raise the limit with ulimit -l in the shell or in the service settings.

Repeat the idle test

Load the model, leave it idle for ten minutes, and send the same prompt again. The time to the first token should now be close to the time right after loading.

Worked example

Worked example: send the prompt “Give me three uses for a paperclip.” right after loading, then again after ten idle minutes. With –mlock, both runs should answer in a similar time; without it, the second run can be several seconds slower while weights are read back from disk.

Expected result

UNTESTED in this guide: no server was run while writing it. The expected result is that the server starts with the flag and the first token after idle time arrives as fast as right after loading.

Limitations

Locking memory reduces what is left for everything else on the machine, so do not use it on a system that is already short on RAM. The flag does not help if the slowdown comes from the model being unloaded, rather than swapped.

Sources

See all