It's based on the official documentation for PyTorch, llama.cpp, ComfyUI and NVIDIA's drivers, all read on 24 November 2026.
If you run AI models on an NVIDIA graphics card, sooner or later you'll see an error that contains the words "CUDA out of memory". It's one of the most common problems in local AI, and it's almost always fixable without buying new hardware. The error means exactly what it says: the app tried to allocate more video memory (VRAM) on your GPU than was free.
The fix depends on which app you're using, but the approach is always the same. Find out what's using the memory, free up what you can, then make the job smaller or move part of it out of VRAM.
Step 1: Check what's using your VRAM
Before changing any settings, check how much video memory your card has and how much is already in use. On Windows, Task Manager gives a quick overview, and our guide on how to check VRAM usage in Windows covers the options in more detail.
For more precise figures, open a terminal and run nvidia-smi. NVIDIA's documentation says its FB Memory Usage section reports the on-board frame buffer memory, which is your card's VRAM, including total, reserved, used and free amounts. It also lists the processes using the GPU.
Look out for:
Other AI apps still running. An Ollama, LM Studio or ComfyUI instance left open in the background can keep a model loaded in VRAM.
Games and GPU-heavy apps. Close them before loading a large model.
A second copy of the same app. It's easy to start a script twice.
Step 2: Understand why PyTorch can look like it's using more memory than it is
Many AI apps, including ComfyUI and most Python projects, are built on PyTorch. PyTorch's documentation explains that it uses a caching memory allocator: when tensors are freed, the memory is kept in a cache for reuse rather than returned to the GPU straight away. That's why nvidia-smi can show more memory in use than an app's tensors actually need.
PyTorch says calling torch.cuda.empty_cache() releases unused cached memory so other GPU applications can use it, but it doesn't free memory still occupied by tensors, and it can't increase the amount available to PyTorch itself. In other words, it helps another app, not the one that's running out.
Step 3: Fix out-of-memory errors in llama.cpp-based LLM apps
For large language models, the biggest VRAM users are the model's weights and the context, the memory set aside for the conversation. llama.cpp's server documentation lists the settings that control both:
-ngl / --n-gpu-layers: the maximum number of model layers to store in VRAM. It accepts an exact number, auto or all, and defaults to auto. Lowering it keeps more of the model in system RAM, which avoids the error at the cost of speed.
-c / --ctx-size: the size of the prompt context. The default of 0 uses the model's own setting, which for some models is very large. A smaller context uses less memory.
--fit: adjusts unset arguments so the model fits in device memory, and is on by default. The --fit-target option sets the margin to leave free on each device (1,024MiB by default).
-ctk / --cache-type-k: the data type for the K part of the context cache. The default is f16, and lower-precision options such as q8_0 use less memory.
--cpu-moe and --n-cpu-moe: for mixture-of-experts models, these keep some or all of the expert weights on the CPU.
If you've set -ngl or a large -c by hand, try removing them and letting --fit choose, or reduce them step by step. Our guides to context length and VRAM and running MoE models on low VRAM with llama.cpp explain both in more depth. Apps built on llama.cpp often expose the same settings under names such as GPU offload and context length.
Step 4: Fix out-of-memory errors in ComfyUI
ComfyUI's README says it uses smart VRAM and RAM management and model offloading, and that it can run large models on as little as 4GB of VRAM. Its command-line options include several that help when you hit memory errors:
--reserve-vram: sets how many gigabytes of VRAM to reserve for your operating system and other software. Useful if ComfyUI shares the card with a browser, games or your display.
--novram: described in ComfyUI's own help text as the option for "when lowvram isn't enough".
--cpu-vae: runs the VAE, the part that decodes the final image, on the CPU.
--disable-smart-memory: forces ComfyUI to offload models to system RAM more aggressively instead of keeping them in VRAM.
ComfyUI's help text also says the old --lowvram option does nothing when its dynamic VRAM management is enabled, so don't expect it to fix the problem on its own. Our guide to running ComfyUI on low VRAM covers these options in detail. Lowering the image resolution or batch size in your workflow also reduces memory use.
Step 5: Tune PyTorch's memory allocator in your own scripts
If you're running your own Python scripts, PyTorch lets you change how its allocator behaves using the PYTORCH_ALLOC_CONF environment variable. PyTorch says PYTORCH_CUDA_ALLOC_CONF is an alias kept for backward compatibility. Two options are relevant:
expandable_segments:True: experimental and off by default. PyTorch says it lets the allocator create memory segments that can be expanded later, which can better handle jobs whose allocation sizes change frequently, such as a changing batch size.
max_split_size_mb: stops the allocator splitting blocks larger than a set size, which PyTorch says can reduce fragmentation and may allow some borderline workloads to finish. It describes this as a last resort for workloads that abort with "out of memory" while showing a large amount of inactive split blocks, and notes the performance cost can be substantial.
For example, on Windows you could run set PYTORCH_ALLOC_CONF=expandable_segments:True in the Command Prompt before starting your script. Reducing the batch size, image resolution or sequence length is still the most reliable fix.
Step 6: Check NVIDIA's system memory fallback setting
On Windows, NVIDIA's drivers have a feature that changes how out-of-memory situations behave. NVIDIA says that from driver 536.40 it introduced a way for applications to use shared system memory when GPU memory is exhausted, which lets apps that previously crashed keep running, albeit more slowly. From driver 546.01, it added a setting called CUDA - Sysmem Fallback Policy in the NVIDIA Control Panel, under Manage 3D settings, to turn that fallback off for a specific program.
That has two consequences. If you've set Prefer No Sysmem Fallback for your AI app, you'll get crashes instead of slowdowns when VRAM runs out, so switch it back to Driver Default if you'd rather the job finished slowly. And if an app suddenly becomes very slow rather than crashing, it may be falling back to system memory, which is a sign you're at the limit of your card. Our guide on how to disable CUDA sysmem fallback shows both settings step by step.
When it's time for more VRAM
If you've freed memory, reduced the context or resolution and offloaded what you can, and the model still doesn't fit or runs too slowly to be useful, the honest answer is that you need a smaller model or a graphics card with more memory. Our guide to how much VRAM you need in 2026 explains what different amounts of memory are good for.
Our verdict
Most CUDA out of memory errors can be fixed in a few minutes: check what's using your VRAM with nvidia-smi or Task Manager, close anything you don't need, then shrink the job. In llama.cpp-based apps, that means fewer GPU layers, a smaller context or a quantised context cache; in ComfyUI, options such as --reserve-vram, --cpu-vae and --novram; and in your own PyTorch scripts, a smaller batch or the expandable_segments allocator setting. Leave NVIDIA's sysmem fallback on Driver Default if you'd rather jobs slow down than crash, and only consider a new graphics card once those fixes run out.