Why Local LLM Memory Spikes During Loading and Then Drops

    When you run a large language model on your own machine, the weight file size is only the starting point — not the number that determines whether the model will fit. This article explains why memory usage shoots up during loading and falls back afterward, using a 13B MoE model with 4-bit quantization on llama.cpp as a reference case. It breaks memory down into three phases (loading, steady state, and inference), separates reclaimable page cache from non-swappable pinned and anonymous memory, quantifies the extra overhead introduced by KV cache, checkpoints, prefill workspaces, and speculative decoding, and offers a practical way to judge whether your hardware can actually host a model. It also covers unit confusion between GB and GiB and common sources of the "memory is 10x the model file" myth.

    Key Takeaways

    The size of the weight file is only the starting point. Whether a large model runs locally is determined not by any single peak reading, and not by the model file itself, but by the unreclaimable working set + inference runtime state. Memory spiking during loading usually comes from pinned memory, file page cache, and anonymous memory accumulating at the same time. The drop after loading completes happens because the page cache is reclaimed, leaving only non-swappable pinned memory and anonymous memory.

    To make this concrete, the article first standardizes the measurement conventions, then breaks down the loading, steady-state, and inference phases, and finally shows how to judge whether a machine can hold the model.

    1. Standardize Your Units First

    Many arguments that “memory usage far exceeds the model file size” actually come from mixing units. If a file is reported as 17.6 GB while the process shows 24 GiB, the two numbers are not in the same unit, and a direct comparison distorts the picture. This article uses GiB throughout. If a tool reports GB, convert with 1 GiB ≈ 1.0737 GB.

    The reference setup used throughout this article:

    • Model: M-13B-MoE, roughly 13B parameters, MoE architecture
    • Quantization: 4-bit
    • Inference engine: llama.cpp b4500, pinned memory enabled
    • Hardware: x86_64 host, 32 GiB physical memory
    • Context length: default 4096 tokens
    • Concurrency: single decode slot, speculative decoding disabled

    Under this setup, the model weight file is 17.6 GiB. The peak during loading is about 25.4 GiB, and the steady-state baseline after loading settles at about 16.0 GiB. Note that a different model, quantization scheme, inference engine, context length, or concurrency setting changes the numbers significantly, though the mechanism stays essentially the same.

    2. Loading Phase: Why the Peak Climbs Past 25 GiB

    The loading-phase peak is the sum of three components.

    2.1 Pinned memory: about 14.8 GiB

    In an MoE model, part of the expert weights is locked into host memory through pinned memory. What makes this allocation special is that the operating system cannot freely swap it out to the swap partition. It is a textbook case of unreclaimable memory.

    In this example, it accounts for about 14.8 GiB. It is also one of the core components that remain resident after loading completes.

    2.2 File-backed page cache: about 9.4 GiB

    During loading, the weight files are read into the operating system page cache. The page cache exists to reduce repeated disk I/O, so it visibly consumes physical memory during the loading phase.

    In this example, it accounts for about 9.4 GiB. It pushes loading-phase memory up quickly, but it does not represent long-term resident memory. Once loading finishes, the kernel can reclaim these clean pages if memory pressure appears, if the mapping is unmapped, or if the inference engine explicitly hints at releasing them.

    2.3 Anonymous memory: about 1.2 GiB

    Anonymous memory covers the process heap and stacks, temporary buffers, and ordinary dynamic allocations created while the inference engine initializes. In this example it is about 1.2 GiB.

    Adding the three together:

    14.8 GiB pinned memory + 9.4 GiB page cache + 1.2 GiB anonymous memory = 25.4 GiB

    That is why loading-phase memory climbs above 25 GiB. It is not that the weight file suddenly inflated tenfold; it is that several kinds of memory coexist while loading is in progress.

    3. After Loading: Why Memory Drops

    Once loading completes, memory usage typically falls well below the peak. In this example, the steady-state baseline is about 16.0 GiB.

    The breakdown:

    • Pinned memory: about 14.8 GiB
    • Anonymous memory: about 1.2 GiB
    • Page cache: drops to nearly negligible once reclaimed

    Therefore:

    14.8 GiB + 1.2 GiB ≈ 16.0 GiB

    This is actually smaller than the 17.6 GiB weight file. The reason is not that the model shrank, but that the page cache no longer counts as long-term usage in the real working set.

    One common misconception needs clarifying here: page cache is not necessarily released automatically the moment it is no longer referenced. A more accurate description is that the kernel may reclaim these clean pages only when the process unmaps the file, when the system comes under memory pressure, or when the inference engine or operating system signals a reclaim through mechanisms similar to fadvise or posix_fadvise. If the machine has plenty of memory, the page cache may stay resident to speed up subsequent accesses.

    So if you observe “memory is still high after loading,” it does not necessarily mean the model’s resident memory is genuinely high — the page cache may simply still be there. In that case you need to distinguish between reclaimable page cache and unreclaimable pinned plus anonymous memory.

    4. What Inference Adds at Runtime

    The 16.0 GiB above is only the steady-state baseline after weights are loaded and the engine performs basic initialization. Actual inference brings runtime state on top of it, commonly including the KV cache, checkpoints, prefill workspaces, output projection heads, and speculative-decoding-related structures.

    The table below lists typical additional items for the same example model. Note that different rows may correspond to different context lengths or to speculative decoding being on or off, so values from different premises must not be naively summed.

    Item Usage Measurement premise / notes
    KV cache (4096 context, single slot) ~352 MiB Grows roughly linearly with context length
    KV cache (8192 context, single slot) ~704 MiB Long-context scenario
    KV ring buffer ~642 MiB Sliding-window / circular context
    Checkpoints (per slot) ~62.8 MiB One per decode slot
    Prefill workspace (short path) ~160 MiB Shorter sequence prefill
    Prefill workspace (long path) ~318 MiB Long sequence prefill
    Output projection head ~515 MiB Vocabulary projection layer
    Multi-token prediction sidecar ~311 MiB Present when speculative decoding is enabled
    Extra pinned memory for multi-token prediction ~292 MiB Speculative decoding enabled, resident and non-swappable
    Multi-token prediction rollback snapshot ~188 MiB Speculative decoding enabled, state needed for rollback

    Estimating only with the most basic setup — 4096 context, single slot, speculative decoding off — the additional runtime state is roughly:

    352 MiB KV cache + 62.8 MiB checkpoints + 160 MiB short-path prefill workspace + 515 MiB output projection head ≈ 1.06 GiB

    Added to the 16.0 GiB weight steady-state baseline, actual inference usage is approximately:

    16.0 GiB + 1.06 GiB ≈ 17.1 GiB

    Enable 8192 context, multiple slots, and speculative decoding, and the memory requirement keeps climbing.

    5. Where the “10x the File Size” Impression Comes From

    The claim that “memory usage is ten times the model file” is usually inaccurate, though it does not come from nowhere. In most cases it is an illusion produced by several factors stacking up.

    5.1 Treating the loading peak as steady-state usage

    The loading peak contains a large amount of page cache. If you take the peak as long-term runtime memory, the model looks like it “eats a lot of extra memory.” In reality, part of that page cache is reclaimable.

    5.2 Pulling the context too long

    The KV cache grows roughly linearly with context length. A 4096-token context and a 32768-token context are not in the same league. Long-context scenarios easily consume several additional GiB.

    5.3 Increasing concurrent slots

    Each decode slot may require its own checkpoints and state. It looks small with a single concurrent request, but scales up multiplicatively with concurrency and batching.

    5.4 Enabling speculative decoding

    Multi-token prediction and speculative decoding bring sidecar structures, extra pinned memory, and rollback snapshots. They improve throughput or latency, but they also increase resident memory pressure.

    5.5 Quantization and dequantization buffers

    Some inference paths need dequantization workspaces on top of a quantized model. This overhead depends on the quantization format, kernel implementation, and engine version, and commonly ranges from a few hundred MiB to several GiB. It must be measured separately; looking at the weight file alone is not enough.

    5.6 Mixing units

    File sizes are commonly given in GB or GiB, and task managers or monitoring tools may mix them too. 17.6 GB and 16.8 GiB are not equivalent, and comparing them directly leads to misjudgment.

    6. How to Judge Whether Your Machine Can Hold It

    To decide whether a local machine can run the model, do not look only at some peak, and do not look only at the file size. Separate memory into two categories instead.

    Reclaimable memory

    The typical example is the page cache produced by file mapping. It can be released when memory pressure rises, and does not necessarily block loading or running the model.

    Unreclaimable memory

    Typical examples include:

    • Pinned weights held in pinned memory;
    • Process anonymous memory;
    • Runtime structures created by the inference engine;
    • Extra pinned memory and snapshots once speculative decoding is enabled.

    In this example, the unreclaimable baseline working set after weights load is about:

    14.8 GiB pinned memory + 1.2 GiB anonymous memory ≈ 16.0 GiB

    Enter basic inference and add roughly 1.06 GiB of runtime state, and the real available-memory requirement is about 17.1 GiB.

    So when judging whether a machine can hold the model, compare “available physical memory” against “unreclaimable working set + runtime state” rather than being frightened by the loading peak.

    One risk still deserves attention: reclaimable page cache does not mean you can push the physical memory allocation to the limit. During inference, the KV cache, workspaces, concurrent state, and temporary buffers can all grow. If the machine has only slightly more memory than the theoretical minimum, it can still hit OOM under long context, high concurrency, or enabled speculative decoding. Reserving 10% to 20% of headroom is advisable.

    7. Practical Recommendations

    1. Measure the loading, steady-state, and inference phases separately.
      Do not capture a single peak. Memory composition differs during loading, after loading completes, and once inference begins.

    2. Look at the unreclaimable portion first.
      Focus on pinned memory, anonymous memory, and runtime structures. Page cache is a useful reference but should not be treated directly as steady-state usage.

    3. Control context length and concurrency.
      The KV cache and slot state are the largest and most easily adjustable variables. Set the context according to actual needs instead of maxing it out.

    4. Enable speculative decoding cautiously.
      It can improve performance, but introduces sidecars, extra pinned memory, and rollback snapshots. Measure it separately before going live.

    5. Unify units before comparing.
      Convert file size, process memory, and tool reports to GiB or GB so that unit differences do not mislead you.

    6. Stress-test on the target hardware.
      Local estimates are only a reference. The final deployment environment should be stress-tested with long context, concurrency, and long-running sessions.

    8. In One Sentence

    The model file size is only the starting point; whether it runs stably locally depends on whether the machine’s physical memory can hold the “unreclaimable working set + runtime state.” Only by understanding the loading peak, the steady-state drop, and the reclaimable page cache mechanism can you genuinely judge whether your local hardware is sufficient.