How to Reduce GPU Memory Use for Local AI Models
In this article
Running AI models locally can quickly exhaust your GPU's VRAM, causing out-of-memory errors, crashes, or frustrating slowdowns. This guide shows you practical, effective ways to reduce GPU memory usage so you can run larger models or multiple tasks on the hardware you already have. You'll learn about quantization, context window limits, offloading, and other settings that directly impact memory consumption. No advanced coding skills are required—just a willingness to tweak a few configuration files and restart your software.
What You'll Need
- A local AI model already installed (e.g., via Ollama, llama.cpp, Hugging Face Transformers, or a GUI like LM Studio).
- Access to the model's configuration file or command-line interface (CLI).
- Basic familiarity with editing text files (like
.yaml,.json, or.py) and running commands in a terminal. - Your GPU model and its VRAM capacity (check with
nvidia-smion Windows/Linux or System Information on macOS). - Enough free disk space to download alternative quantized versions of your model (usually a few GB).
Step-by-Step
- Identify the biggest memory hogs. Before changing anything, run your model once and monitor GPU memory usage. On Windows, open Task Manager > Performance > GPU. On Linux, use
nvidia-smiin a terminal. Note the peak VRAM usage—this gives you a baseline to measure improvements. - Lower the context window size. The context window (often called
n_ctxormax_seq_len) determines how much text the model can “remember” at once. Reducing it from 4096 to 2048 tokens can cut memory usage by 20–30% in many models. Look for this setting in your model’s config file or in the launch command (e.g.,--ctx-size 2048for llama.cpp). - Switch to a quantized version of your model. Quantization reduces the precision of the model’s weights (e.g., from 16-bit floats to 8-bit or 4-bit integers), which dramatically lowers VRAM usage. Most model repositories on Hugging Face offer quantized versions (look for files named
Q4_K_M.ggufor8bit). Download a 4-bit or 5-bit quantized version and load that instead of the full-precision model. You’ll lose a little accuracy, but for most tasks the difference is negligible. - Enable GPU offloading with CPU fallback. If your GPU runs out of memory, you can offload some layers to the CPU. Tools like llama.cpp and Ollama allow you to specify how many layers to keep on the GPU. For example, with llama.cpp, use the
--n-gpu-layersflag; set it to a number lower than the total layers so the rest run on CPU. This trades speed for lower VRAM usage, but it lets you run models that would otherwise crash. - Use a smaller batch size. For inference, the batch size (how many tokens are processed at once) affects memory. In Transformers pipelines, set
batch_size=1or2instead of the default. In llama.cpp, add--batch-size 1to your command. This is especially helpful if you're processing multiple prompts in a script. - Close other GPU applications. Browsers, video players, and even other AI processes consume VRAM. Close unnecessary programs, especially those that use hardware acceleration. You can also check
nvidia-smito see which processes are using GPU memory and kill the ones you don't need (usingTask Managerorkillcommand). - Update your GPU drivers and AI framework. Newer drivers and libraries often have better memory management. Visit the official NVIDIA/AMD/Intel driver page and update. Also update your AI software (e.g.,
pip install --upgrade transformersor update Ollama). This is a simple step that can fix memory leaks. - Set environment variables to limit memory (if using PyTorch). In PyTorch, you can set
PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:128before running your script. This forces PyTorch to use smaller memory blocks, reducing fragmentation and overall usage. For example, in a terminal:export PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:128(Linux/macOS) orset PYTORCH_CUDA_ALLOC_CONF=max_split_size_mc:128(Windows). - Use a model with fewer parameters. If you're still struggling, consider switching to a smaller model. For instance, instead of a 13B parameter model, try a 7B or 3B version. These use significantly less VRAM and often run faster on consumer GPUs.
- Restart your AI application after changes. Most memory settings are read at startup. After editing config files or changing environment variables, fully quit and relaunch your software to apply the changes.
Common Mistakes
- Ignoring the context window. Many users leave the default context at 4096 or higher, even for short tasks. Reducing it is the single easiest win.
- Using a quantized model that's too aggressive. A 2-bit quantized model saves a lot of VRAM but may produce gibberish. Stick to 4-bit or 5-bit for a good balance.
- Not checking the actual VRAM usage. Guessing is unreliable. Always monitor with
nvidia-smior Task Manager before and after changes. - Forgetting to restart the application. Changing a config file while the app is running has no effect until you restart.
- Overlooking CPU offloading. Some users think CPU offloading is only for when the GPU is full, but it can also be used to free up VRAM for other tasks.
Tips
- Keep a log of the settings you change and the resulting VRAM usage. This helps you find the optimal configuration without trial-and-error every time.
- If you use Ollama, you can set the context size per model in the Modelfile (e.g.,
PARAMETER num_ctx 2048). - For Transformers, enable
torch.compile()if you have PyTorch 2.x—it can reduce memory usage by a few percent. - Consider using a tool like
llama.cppwith its built-in memory mapping (--mlockor--no-mmap).--no-mmaploads the model fully into RAM, which can reduce VRAM spikes. - If you have multiple GPUs, you can split the model across them (e.g., with
--tensor-splitin llama.cpp). This uses more total VRAM but prevents a single GPU from hitting its limit.
Troubleshooting
- Model still crashes with “CUDA out of memory.” Try reducing the context window further (e.g., to 1024) and use a 4-bit quantized model. If it still crashes, offload all layers to CPU (
--n-gpu-layers 0) to confirm the model itself works, then gradually increase GPU layers. - Performance becomes very slow after offloading. That's expected because CPU is slower. Balance the number of GPU layers—start with half the total layers and adjust. Also ensure your CPU has enough RAM (at least 16GB).
- Changes have no effect. Double-check that you're editing the correct config file. Some applications have multiple configs (e.g., for the UI and the backend). Also, verify the setting name is correct for your software version.
- Quantized model fails to load. Ensure you downloaded the correct file format for your software (e.g., GGUF for llama.cpp, safetensors for Transformers). Also check that the quantized model is compatible with your model architecture.
- GPU memory usage is still high after closing other apps. Some background processes (like Windows Desktop Window Manager) use VRAM. You can restart your computer to clear all GPU memory, then start only your AI app.
Frequently Asked Questions
What is quantization and why does it reduce VRAM?
Quantization stores model weights in lower precision (e.g., 4-bit integers instead of 32-bit floats). This reduces the memory required to store each weight, cutting total VRAM usage significantly—often by 75% or more compared to full precision.
Will reducing the context window affect response quality?
Yes, but only for tasks that need long-term memory, like summarizing long documents. For short Q&A or code generation, a context of 2048 tokens is usually enough. You'll notice truncation if you exceed the limit.
Can I run a 13B model on an 8GB GPU?
Yes, with a 4-bit quantized version and a small context window (e.g., 2048), a 13B model can fit in 8GB VRAM. You may need to offload a few layers to CPU to avoid OOM errors.
Is it safe to overclock my GPU to get more memory?
Overclocking does not increase VRAM capacity; it only increases clock speeds. It can cause instability and may affect your warranty. It's not a recommended solution for memory issues.
Why does my GPU memory usage increase over time?
This could be a memory leak in the AI framework or your application. Update your software, and if the issue persists, try restarting the app periodically. Also, check if you have multiple processes running the same model—kill duplicates.
What's the difference between CPU offloading and using a smaller model?
CPU offloading keeps the model intact but runs some layers on CPU, saving VRAM but slowing down inference. Using a smaller model reduces total parameters, which also reduces VRAM but may lower output quality. Offloading is better if you need the larger model's accuracy.