Free | No tracking | Every fact with a source|German and English

Error / Memory

llama.cpp: CUDA error: out of memory

llama.cpp crashes while loading or during the response because there isn’t enough graphics memory for the model and context.

The message

ggml_backend_cuda_buffer_type_alloc_buffer: allocating … MiB on device 0: cudaMalloc failed: out of memory … CUDA error: out of memory

Affects: llama.cpp, KoboldCpp, LM Studio

Why is this happening?

You assigned more layers to the GPU with -ngl or requested a larger context with -c than fits in VRAM. The context memory (KV cache) grows with context length and can take up several gigabytes with long contexts. Other programs on the graphics card further reduce the free space.

How to fix it

  1. Update llama.cpp. Newer versions automatically adjust unset values to the available memory with --fit (on by default). Leave out -ngl and -c instead of setting large fixed values.
  2. Reduce the context length, for example -c 8192.
  3. Put fewer layers on the GPU, for example -ngl 30 instead of -ngl 99, and work your way up until the error comes back.
  4. Enable Flash Attention with -fa on and shrink the KV cache with -ctk q8_0 -ctv q8_0.
  5. For MoE models: With --n-cpu-moe N, you put the expert weights of the first N layers on the CPU, while the rest stays fast on the GPU.
  6. Use a smaller quantization (for example Q4_K_M instead of Q6_K) and close other programs that use VRAM.
  7. KoboldCpp: Lower the context size (--contextsize) and GPU layers, and also reduce --blasbatchsize to 128 or 64.

Set up from scratch again

If it still doesn’t work after that, a fresh setup step by step often helps:

Terms for this

Similar errors

Sources

Checked on Oct 21, 2026. Didn't help? Ask in the forum and name the graphics card, program, and the full message.