On this page
“cudaMalloc failed: out of memory” means your NVIDIA card ran out of VRAM while loading the model. The weights, the conversation cache (the KV cache) and anything else on the card have to fit at once. Fix it in this order: shrink the context, put fewer layers on the GPU, pick a smaller quant, then close whatever else is holding VRAM. If the model still won’t fit, it’s too big for that card, and you need a smaller model or a bigger machine.
The same line shows up in Ollama, LM Studio and llama.cpp, because all three load GGUF models through llama.cpp. The commands below are from Ollama’s and llama.cpp’s own docs, checked on 16 November 2026.
What the error is telling you
A running model needs three things in memory:
| What uses VRAM | What makes it bigger |
|---|---|
| The model weights | More parameters, or a higher-precision quant (Q8 is bigger than Q4) |
| The KV cache | A longer context window, more parallel requests |
| Everything else on the card | Your browser, games, Windows itself, a second model still loaded |
The error usually fires when the context grows, so a model that loaded fine yesterday can fail today after you raised the context length. A quick size check: a 4-bit GGUF file is roughly 0.6 GB per billion parameters, and a running model needs more than its file size once the cache is added.
Fix it in Ollama
1. See where the model landed. Run ollama ps. The PROCESSOR column says 100% GPU, 100% CPU, or a split like 48%/52% CPU/GPU. A split means the model only partly fits in VRAM.
2. Lower the context length. Ollama’s docs say a larger context needs more memory. On cards under 24 GB of VRAM, Ollama defaults to a 4k context. If you raised it for coding tools or agents, bring it back down. In the Ollama app, use the context length slider in Settings. Without the app, set OLLAMA_CONTEXT_LENGTH before the server starts.
On Windows, Ollama reads environment variables from your user account:
- Quit Ollama from the tray icon.
- Open the environment variables editor from the Start menu and add
OLLAMA_CONTEXT_LENGTHwith a value such as4096to your user variables. - Start Ollama again from the Start menu.
3. Shrink the KV cache. Set OLLAMA_KV_CACHE_TYPE to q8_0. Ollama’s FAQ says q8_0 uses about half the memory of the default f16, and q4_0 about a quarter, with more quality loss at long contexts. It needs Flash Attention, which Ollama turns on by itself when your card supports it. This setting applies to every model.
4. Unload models you’re not using. Ollama keeps a model in memory for 5 minutes after the last request, and it can hold more than one. Run ollama stop <model> for each one you don’t need.
5. Pull a smaller quant or a smaller model. If you’re on a Q8 or Q6 tag, try the Q4 version of the same model. If a 14B model is failing on an 8 GB card, drop to the 7B or 8B version.
Fix it in llama.cpp
llama.cpp gives you the same levers as flags on llama-server and llama-cli:
| Flag | What it does | Try |
|---|---|---|
-c, --ctx-size |
Context size. 0 loads the model’s own maximum, which can be huge | -c 4096 |
-ngl, --n-gpu-layers |
Layers kept in VRAM. Default is auto |
Lower it, for example -ngl 20 |
-fa, --flash-attn |
Flash Attention, default auto |
-fa on |
-ctk / -ctv |
KV cache type, default f16 | -ctk q8_0 -ctv q8_0 |
--fit |
Adjusts any settings you didn’t set so they fit in device memory. On by default | Leave it on, and don’t pin -c or -ngl higher than you need |
-ncmoe, --n-cpu-moe |
For mixture-of-experts models, keeps expert weights of the first N layers on the CPU | Raise N until it loads |
Two habits cause most failures here. One is passing -c 0 or a big -c on a model with a long native context. The other is forcing -ngl all on a model that’s bigger than your card. With current builds, the simplest fix is often to remove both flags and let --fit choose.
Close what else is using the card
Open Task Manager, go to Performance, and pick your GPU. Dedicated GPU memory shows what’s in use before you load anything. A browser with many tabs, a game launcher or a second AI app can hold gigabytes. Close them, then load the model again.
Why it sometimes gets slow instead of failing
Recent NVIDIA drivers on Windows can spill over into system RAM (“shared GPU memory”) when VRAM runs out. Then you don’t get the error, but replies crawl. The setting is CUDA - Sysmem Fallback Policy in NVIDIA Control Panel under Manage 3D settings. If you’d rather get a clear error than a silent slowdown, set it to Prefer No Sysmem Fallback and restart the AI app. Either way, the real fix is the same list above.
Symptom, check, fix
| Symptom | Check | Fix |
|---|---|---|
| Error right at load | Model file size vs your VRAM | Smaller quant or smaller model |
| Loads, then fails on a long chat | Context length | Lower context, q8_0 KV cache |
ollama ps shows a CPU/GPU split |
VRAM is full | Fewer layers on GPU is fine, but expect slower replies |
| Very slow, no error | Shared GPU memory in Task Manager is climbing | Same fixes, or turn off sysmem fallback |
| Failed after an update | Ollama or llama.cpp version | Check the project’s GitHub issues for your version |
A simpler route on the same PC
If you’d rather not edit environment variables, Off Grid AI Desktop is a free app that runs GGUF models on your PC with llama.cpp. Its Models screen shows memory guidance for each model and marks ones that are Tight on RAM or Won’t fit, before you download. It has a GPU layers slider, KV cache and FlashAttention settings, and a context window per model, and Settings → GPU performance installs the NVIDIA CUDA pack from inside the app. It can’t make a 30 GB model fit an 8 GB card either, but it tells you before you waste the download.
Need the full-size model?
Sometimes the model you want just doesn’t fit consumer hardware, and a smaller quant isn’t good enough for the job. That’s what Off Grid AI Boost is for. It’s an API for open models running on our GPUs, at one flat price from $4.99 a month for the first cohort (regular price $9.99, plans up to $199.99). It names Qwen 3.8 and DeepSeek V4.1 Flash, gives you one API key for every model, and we don’t store your prompts or train on them. A full privacy policy comes with launch.
Boost is a pre-order. Boost starts in January; pre-order to lock in half price. Usage limits for each plan aren’t published yet. Keep the small model on your own card for private, offline work, and use Boost when you need the big one.
FAQ
Why do I get “cudaMalloc failed: out of memory” when Task Manager shows free VRAM? The model needs one more large block than what’s left, and other apps may be holding memory in small pieces. Close them and lower the context, then try again.
Does lowering GPU layers fix CUDA out of memory? Yes, it moves part of the model to system RAM so it loads. Replies get slower, because the CPU does that part of the work.
What context length should I use on an 8 GB card?
Start at 4096, which is Ollama’s own default for cards under 24 GB. Raise it in steps and stop when ollama ps stops showing 100% GPU.
Is Q4 much worse than Q8? Q4 uses about half the memory. For chat and writing most people can’t tell the difference on everyday tasks. Test both on one real task of yours.
Can I add my laptop’s RAM to the GPU? Only by offloading layers, which is slower. There’s no setting that turns system RAM into VRAM at full speed.
Start with one change: lower the context to 4096 and load the model again. That alone fixes most of these errors.