Local LLM Inference: Diagnosing Memory Exhaustion and Resource Limits
Running a quantized local LLM can consume far more memory than its weights alone suggest. This article explains how to estimate memory requirements, understand swap-related slowdowns, distinguish OOM events from driver or hardware failures, and begin investigating crashes with Linux kernel logs.
Running a 4-billion-parameter, 4-bit quantized open-source language model on a laptop can fill physical memory surprisingly quickly. The system may then rely heavily on swap, becoming so unresponsive that a forced restart seems like the only option—potentially taking unsaved work with it.
Although a black screen or system freeze may appear to happen without warning, there is often a traceable cause. Start with a memory budget, then distinguish out-of-memory events from cgroup limits, driver failures, and hardware problems.
1. Build a Memory Budget First
Model weights
Model weights are the first major allocation. At 4-bit precision, each parameter requires approximately 0.5 bytes, so a 4B model has a baseline weight size of about 2 GB.
Quantization formats also store scaling factors, and some layers typically remain at higher precision. As a result, both the model file and its loaded memory footprint will be somewhat larger than this baseline.
KV cache
The second major allocation is the KV cache. During autoregressive generation, the model stores keys and values for tokens in both the input and the generated output.
Estimate its size with:
KV cache size in bytes =2 × number of layers × number of KV heads × head dimension× sequence length × batch size × bytes per element
The factor of two accounts for both keys and values.
For example, consider a model with:
- 36 layers
- 8 KV heads
- A head dimension of 128
- FP16 cache storage, using 2 bytes per element
This configuration requires approximately 144 KiB per token. At a sequence length of 8,192 tokens and a batch size of one, the KV cache occupies about 1.1 GiB.
Cache memory grows linearly with sequence length and batch size. Use the values in the model’s configuration file rather than assuming that all models of a similar parameter count have the same cache requirements.
Inference framework overhead
The inference framework adds its own memory requirements. Initialization, computational graph construction, and temporary buffers used during tensor operations all contribute to peak usage.
The operating system and other applications
The operating system, browser, IDE, and background services also need memory. On a laptop with 16 GB of RAM, these may already occupy several gigabytes before inference begins.
Taken together, these allocations can easily push peak memory usage for a 4B 4-bit model above 8 GB when using a longer context.
For meaningful comparisons, record the following alongside every measurement:
- Model name
- Quantization format
- Context length
- Batch size
- Framework version
- Memory metric being measured
On Linux, monitor the process’s resident set size, or RSS. When using an NVIDIA GPU, also monitor GPU memory usage. On newer kernels with cgroup v2, memory.peak can report the peak memory usage of the control group.
2. Why Swap Does Not Necessarily Cause a Freeze
Swap moves inactive memory pages to disk, freeing physical memory for programs that need it. A few hundred megabytes of swap usage is common in everyday workloads and does not, by itself, normally cause a system-wide black screen.
The problem is that LLM inference repeatedly accesses large amounts of model weights and cached data. If those pages are swapped out, computation threads must frequently wait for disk reads and writes. Even an NVMe SSD has access latency orders of magnitude higher than RAM.
Swap therefore acts more like an amplifier of memory pressure than a standalone explanation for a crash. It can prolong the shortage, slow the system, make the desktop unresponsive, and increase the likelihood of other failures being triggered.
When investigating a black screen, treat swap usage as one clue—not a diagnosis. Check the logs as well.
3. Distinguish OOM Events from Other Failures
Several different conditions can follow severe memory pressure. They require different interpretations.
An inference process killed by the OOM killer.
The kernel terminates the inference process while the rest of the system remains operational. Logs may contain messages such asOut of memoryoroom-kill.A cgroup-level OOM event.
The process runs inside a control group with a memory limit. When that limit is exceeded, processes within the affected group may be terminated. The logs include the relevant cgroup path.A kernel hung-task warning.
A task remains in an uninterruptible state for an extended period, prompting a warning such asblocked for more than 120 seconds. This is commonly associated with blocked disk I/O or driver problems; it is not a direct indication of memory exhaustion.A driver or hardware failure.
GPU driver errors, such as NVIDIA Xid events, thermal protection, unstable power delivery, or faulty RAM can all present as a black screen.A system-level crash.
A kernel panic or machine check exception (MCE) can leave dedicated diagnostic records.
Do not attribute a black screen to a single cause without supporting log evidence.
4. Begin the Crash Investigation with the Previous Boot’s Logs
After restarting, the first step is to inspect the kernel logs from the previous boot. On Linux, run:
journalctl -k -b -1
