How to Choose RAM and VRAM for Running Llama Locally
In this article
To run Llama (or any large language model) locally, you need enough RAM and VRAM to hold the model weights and the context window. The right amount depends on the model size, quantization, and your hardware. This guide walks you through the calculation and selection process.
What You'll Need
- Your GPU model (or integrated graphics) and its VRAM capacity
- Your system RAM capacity
- The Llama model you want to run (e.g., Llama 3.1 8B, Llama 3 70B)
- Knowledge of quantization levels (Q4_K_M, Q5_K_M, Q8_0, etc.)
- A tool like Ollama, LM Studio, or llama.cpp to check memory usage
Step-by-Step
- Identify the model size. Llama models are named by parameter count: 7B, 8B, 13B, 70B, etc. The file size on disk (in GB) is your starting point. For example, a 7B model in FP16 is about 14 GB; in Q4_K_M it's about 4.4 GB.
- Choose a quantization level. Quantization reduces file size and memory usage. Common levels: Q4_K_M (good balance), Q5_K_M (better quality, slightly larger), Q8_0 (near lossless, larger), and FP16 (full precision, largest). Lower quant = less memory but lower quality.
- Calculate VRAM needed. For GPU inference, the model weights must fit in VRAM. Add the model file size (in GB) plus the context window overhead. A safe rule: for a 4K context, add about 1–2 GB; for 8K, add 2–4 GB; for 32K, add 8–16 GB. Use this formula: VRAM needed ≈ model size (GB) + context overhead (GB).
- Check your GPU's VRAM. If you have an NVIDIA GPU, open Task Manager (Ctrl+Shift+Esc) > Performance > GPU, and look at 'Dedicated GPU memory'. For AMD or Intel, use their respective tools (Adrenalin, Arc Control).
- If VRAM is insufficient, use CPU offloading. You can run part of the model on the GPU and part on system RAM. Tools like llama.cpp and Ollama support this via the
--n-gpu-layersparameter (or in LM Studio, the 'GPU Offload' slider). The more layers you offload to GPU, the faster it runs, but you need enough system RAM to hold the rest. - Calculate system RAM needed. For CPU-only inference, you need RAM equal to the model file size plus context overhead. For GPU offloading, you need RAM for the portion of the model not on the GPU, plus the OS and other apps. A safe minimum: 16 GB for 7B models, 32 GB for 13B, 64 GB for 70B (if you plan to run them at all).
- Test with a small context first. Start with a short context (e.g., 512 tokens) and gradually increase. Watch memory usage in Task Manager or your tool's stats. If you get out-of-memory errors, reduce context or lower quantization.
- Consider using a tool that auto-detects. Ollama and LM Studio can automatically set the number of GPU layers based on your VRAM. Use their default settings first, then adjust manually if needed.
Common Mistakes
- Ignoring context overhead. Many users only count the model file size, forgetting that the context (prompt + generated tokens) also occupies memory. A 32K context can add 8+ GB.
- Using too high a quantization. Choosing Q8_0 over Q4_K_M can double memory usage with only a marginal quality increase. Start with Q4_K_M for most uses.
- Assuming more VRAM is always better. If you have limited VRAM, you can still run models by offloading to CPU, but speed drops significantly. It's better to pick a model that fits entirely in VRAM.
- Forgetting about shared memory. On some systems, the GPU can use system RAM as shared memory, but this is slow and can cause crashes. Don't rely on it.
- Not checking the actual file size. Model file sizes vary widely by quantization. Always download the exact file and check its size before planning.
Tips
- Use a model that fits in VRAM. For a 8GB GPU, a 7B model in Q4_K_M (about 4.4 GB) fits with room for context. For a 12GB GPU, you can run 13B Q4_K_M (about 7.5 GB) comfortably.
- Prefer Q4_K_M as a starting point. It's the sweet spot for quality vs. memory. Only go higher if you have extra VRAM.
- Monitor memory in real time. Use Task Manager or nvidia-smi on Windows/Linux to see VRAM usage while running. This tells you exactly how much you need.
- Use the '--num-gpu-layers' flag in llama.cpp. This lets you control how many layers go to GPU. Start with a high number and reduce if you get OOM.
- Consider using a smaller model. If you're tight on memory, a 1B or 3B model can run on almost any modern PC with 8GB RAM.
Troubleshooting
- Out of memory (OOM) error. Reduce the context size, lower the quantization (e.g., from Q5 to Q4), or offload fewer layers to GPU. If using Ollama, set
OLLAMA_CONTEXT_LENGTHto a smaller value (e.g., 4096). - Slow generation. If you're offloading to CPU, generation will be slow. Try to fit the model entirely in VRAM by using a smaller model or lower quant. Also ensure your CPU has enough RAM and is not swapping.
- Model fails to load. Check that you have enough system RAM. For a 7B Q4 model, you need at least 8GB free. Close other applications.
- GPU not detected. Make sure you have the latest GPU drivers. In llama.cpp, use
--list-gpusto see if it detects your card. If not, reinstall drivers or use CPU-only mode. - System freezes or crashes. This often happens when you exceed VRAM and the system tries to use shared memory. Reduce context or quant. Also ensure your power supply can handle the GPU load.
Frequently Asked Questions
How much VRAM do I need for a 7B model?
For a 7B model in Q4_K_M (about 4.4 GB), you need at least 6 GB of VRAM to have room for context. For 8K context, 8 GB is safer.
Can I run Llama on CPU only?
Yes, but it will be slow. You need enough system RAM to hold the model and context. For a 7B Q4 model, 8 GB RAM is the minimum, but 16 GB is recommended for comfort.
What is the difference between RAM and VRAM?
RAM is system memory used by the CPU; VRAM is dedicated memory on the GPU. For running LLMs, VRAM is faster but limited; RAM is larger but slower.
Does a bigger context window require more memory?
Yes, the context window (tokens you input + output) is stored in memory. Each token uses roughly 2 bytes per layer, so a 32K context can add several GB.
Can I use integrated graphics (iGPU) to run Llama?
Yes, but iGPUs share system RAM, which is slower. It works for small models (1B–7B) but expect slow speeds. You need at least 16 GB of system RAM.
Should I buy more VRAM or more RAM?
If you plan to run larger models, VRAM is the bottleneck. But if you already have a GPU with 8–12 GB, adding system RAM can help with offloading. For most users, a GPU with more VRAM is the better investment.