Fix "out of memory" errors in KoboldCpp
Out of memory means the model weights, the context (KV cache) and working buffers together need more VRAM or RAM than you have. Lower the context size first, then try --quantkv q8_0, then let autofit choose GPU layers (--gpulayers -1) or set fewer by hand, then use a smaller quant. The numbers below show which part is too big for your setup.
Checked against KoboldCpp 1.124.2 · updated 13 Nov 2026
- lab-lnx: Linux test VM · 4 vCPU · 15 GB · no GPU
- gha-mac15: macOS 15 test VM (Apple Silicon) · 3 vCPU · 7 GB · paravirtual Metal
- sw-vulkan: Software Vulkan (Mesa lavapipe) · reports exact VRAM allocations, speeds meaningless
On this page
The interactive memory calculator is built with the memory calculator page; until then, the formula and measured sizes below let you work it out.
KoboldCpp out of memory: which message do you have?
| cudaMalloc failed: out of memory | CUDA | Load, or a long chat | users report | While loading · row |
| ErrorOutOfDeviceMemory | Vulkan | Load | users report; not reproducible on our software GPU (F54) | While loading · row |
| Device memory allocation of size N failed | Vulkan | Load | users report | While loading · row |
| failed to allocate compute (pp) buffers | Any | First prompt | users report | While loading · row |
| failed to allocate buffer for kv cache | Any (we saw it on CPU RAM) | Load | tested, F55 | System RAM · row |
| CUDA error: an illegal memory access was encountered | CUDA | Long chat | users report | Chat gets long · row |
| failed to allocate … pinned memory | CUDA | Load | users report; harmless | row |
We could only reproduce one of these ourselves. Our lab has no GPU with a limited memory, and forcing small Vulkan limits did nothing on the software GPU F54, so the GPU messages are reported by users, not tested. The one we reproduced ran out of system RAM:
ggml_backend_buft_alloc_buffer_n_default: failed to allocate CPU buffer of size 64455966721llama_init_from_model: failed to initialize the context: failed to allocate buffer for kv cache2gpttype_load_model: error: failed to load model '<models>/qwen2.5-0.5b-instruct-q4_k_m.gguf'3- ggml_backend_buft_alloc_buffer_n_default: failed to allocate CPU buffe: The allocation that failed: 6 GiB for the context cache.
- llama_init_from_model: failed to initialize the context: failed to all: The real error.
- gpttype_load_model: error: failed to load model '<models>/qwen2.5-0.5b: A wrapper: the load stopped. KoboldCpp exited with code 3.
What fills your memory
Four things share your memory: the model weights, the KV cache, the compute buffer and any extra models. This is Llama 3.1 8B Q4_K_M with all 33 layers on the GPU, from allocations we logged F36. On an 8 GB card, 8K context fits and 32K does not:
- Model weights 3.9 GB
- KV cache 1.0 GB
- Compute buffer 124 MB
- Free 2.9 GB
77 MB doesn't fit on this device
- Model weights 3.9 GB
- KV cache 4.0 GB
- Compute buffer 148 MB
Values are MiB measured from KoboldCpp’s own log lines (the card size is nominal; some memory is used by your system). A further 692.8 MiB token table and 266.5 MiB of buffers stay in system RAM.
- Weights are about the file size: 3,992.5 MiB for a 4.58 GiB file once the token table stays on the CPU.
- KV cache grows in a straight line with context. The formula matched our logged sizes exactly: 51, 195 and 1,539 MiB at 4K, 16K and 128K on our 0.5B model F10, and 1,056 and 4,128 MiB on the 8B model F36. KoboldCpp adds 256 tokens to the context you ask for F06.
- Compute buffer depends on batch size and the model: 124.3 MiB at 8K and 148.3 MiB at 32K here.
Data
| Item | Value (MB) |
|---|---|
| 2K context | 616 |
| 8K context | 688 |
| 32K context | 976 |
| 128K context | 2123 |
Download CSV · Results: R-run1-lnx-nocuda-ctx-2048, R-run1-lnx-nocuda-ctx-8192, R-run1-lnx-nocuda-ctx-32768, R-run1-lnx-nocuda-ctx-131072-q4
Generation speed stayed within ±10% from 2K to 128K F11. RAM numbers from a CPU lab say nothing about VRAM; the shape is what matters.
Out of memory while loading
Work through these in order and change one thing at a time. After each change, reload and check the KV buffer size line.
- Lower the context size. The default is 16384 F05. On the 8B model, 8K needs 1,056 MiB of cache, 32K needs 4,128 MiB (calculated from measured allocations). See context size.
- Quantise the KV cache with KV-cache data type: f16 (default), bf16, q8_0, q5_1 or q4_0. Quantizing saves memory; without flash attention only K is quantized. Old numeric values 0-3 still accepted.Default f16Launcher Context tab: Quantize KV Cache (slider)Since v1.67All details
q8_0orq4_0. In our CPU lab, peak RAM at 32K went from 976 to 794 to 698 MB F10. We have not yet measured GPU savings or quality. - Let autofit choose the layers.
--gpulayers -1is the default and leaves a margin: Spare VRAM in MB that autofit leaves free as a safety margin (default 1024).Default 1024Launcher Hardware tab: Autofit Padding (MB)Since v1.108All details is 1024 MB in our logs. Or set fewer layers by hand: GPU layers. - Use a smaller quant: quantization.
- Lower the batch size (Logical batch size for prompt processing (default 512). Larger is faster but uses more memory; -1 turns batching off. Formerly --blasbatchsize.Default 512Launcher Hardware tab: Batch Size (slider)Since v1.8All details, default 512). We have not measured the memory saved.
- Keep the cache in RAM with Keeps the KV cache in system RAM instead of VRAM (llama.cpp -nkvo). Fits more layers but is slow.Default falseLauncher Hardware tab: No KV offloadSince v1.100All details. It is slower.
To undo any of these, remove the flag or reset the launcher to its defaults.
Out of memory when the chat gets long
The cache fills as the chat grows. If the context is set higher than your card can hold, the failure comes only after many turns. Users report illegal memory access crashes with a quantised cache and ContextShift (#2088 with 60 comments, #1469, #1412); we have not reproduced them. Lower the context, return to --quantkv f16, and update, because several of these were fixed in later releases. See context size and illegal memory access.
Image, vision and voice models need memory too
A vision projector, a Whisper model, a TTS model and an image model each take their own memory. Move them to the CPU where KoboldCpp allows it, for example Keeps the vision/audio projector on the CPU even when the main model is on GPU.Default falseLauncher Loaded Files tab: V.Force CPUSince v1.90All details, or leave them out while you load a large model. Users report that autofit accounts for loaded extras (#2471); we have not confirmed it. See image generation, vision and voice.
Windows: “shared GPU memory” instead of an error
On Windows, the NVIDIA driver can let a model spill into system RAM instead of failing. There is no error, but speed drops sharply. Users report this on #1413 (25 comments). We have not measured it, because we have no NVIDIA GPU. The driver setting “CUDA - Sysmem Fallback Policy” can be set to “Prefer No Sysmem Fallback”, which gives a clear error instead of a slow run (users report).
Out of system RAM (not VRAM)
Layers that are not on the GPU live in system RAM, and so does the weights file on a CPU start. The log prints Detected Available RAM: … MB, for example 15,282 MB on our lab machine. With an enormous context, the cache itself can fail to fit: a 524,288-token context needs 6,147 MiB of KV cache even on the 0.5B model, and the load failed with the lines above F55. Close other programs, lower the context, or use a smaller file. The mmap option is off by default, and our test shows turning it on lowers only the reported working set on Windows F12.
Mac: unified memory limits
Metal can use only part of the unified memory, which users report as about 65 to 75% by default (#24); we have not measured it. Our macOS runner is a virtual machine, so its speeds say nothing about a real Mac F29. See the Mac guide.
Several GPUs
If one card runs out while another has room, set a tensor split with --tensor_split. Users report this on #1882. See multi-GPU.
Not sure what is wrong? Start the decision tree. Slow, not failing? Speed.
Frequently asked questions
How much VRAM does context use?
The KV cache is 2 (keys and values) x layers x context cells x KV heads x head size x bytes per value. For Llama 3.1 8B at 8K we measured 1,056 MiB and at 32K 4,128 MiB, and the formula gives exactly those figures. KoboldCpp adds 256 tokens to the context you ask for.
Does quantising the KV cache hurt quality?
We have not measured output quality yet. It saves a lot of memory at long context: at 32K on our small model peak RAM went from 976 MB to 794 MB with q8_0 and 698 MB with q4_0. Users also report that a quantised cache turns ContextShift off. Test your own output after you change it.
Why does KoboldCpp run out of memory after a long chat?
The cache fills as the chat grows. If the context size is set higher than your card can hold, memory runs out only once the chat gets long. Lower the context size, or use a smaller KV cache type.
Is "failed to allocate pinned memory" a problem?
Usually not. It means KoboldCpp fell back to ordinary memory and keeps running. Users report it as harmless unless you use --usemlock.
Can KoboldCpp use system RAM when VRAM is full?
Yes. Layers that are not offloaded run on the CPU from system RAM. It works but generation is slower, see GPU layers.