Skip to content

[Bug/Feature] Idle weight TTL unloads components but retains ~4.2 GiB VRAM (128 MiB after restart) #2411

Done

Description

@ampersandru

Problem

With the new idle weight TTL support, model-component unloading runs successfully, but sd-server retains roughly 4.2 GiB of VRAM afterward. Immediately after restarting the same container, before generating, it uses only approximately 128 MiB.

Is this retained memory expected (for example, a CUDA backend pool or other runtime allocations), or is there something the residency TTL should also release?

Environment

  • Official Docker image: ghcr.io/leejet/stable-diffusion.cpp:master-cuda
  • Server log: stable-diffusion.cpp version unknown, commit ce7ef90
  • GPU: NVIDIA GeForce RTX 5060 Ti, 16 GB
  • Host driver: 595.112.07 (nvidia-smi reports CUDA 13.2 driver support)
  • The published image uses CUDA runtime 12.6.3.
  • GPU selected using CUDA_VISIBLE_DEVICES=1, exposed inside the container as cuda0.
  • Qwen Image 2.1 Turbo Q8 GGUF, Qwen3-VL 8B Q4 text encoder, vision projector and Qwen-compatible VAE; auto-fit and flash attention enabled.
  • --weight-residency-ttl 120; also tested with --weight-staging-ttl enabled, with the same remaining VRAM.

Reproduction

  1. Start sd-server with a model configured through command-line arguments and --weight-residency-ttl 120.
  2. Check nvidia-smi before generating: approximately 128 MiB attributed to sd-server after a fresh restart.
  3. Generate an image and let generation finish.
  4. Leave the server idle for more than 120 seconds. The verbose log confirms cached model components unload.
  5. Check nvidia-smi: the server still retains about 4.2 GiB. Restarting returns its usage to approximately 128 MiB.

Observed process memory

State VRAM attributed to /sd.cpp/bin/sd-server
Fresh restart, before generation ~128 MiB
During generation 11,628 MiB
After idle unloading 4,294 MiB

The idle screenshot showed 4,475 MiB total on the GPU, including a separate ignis-server process using 158 MiB; the 4,294 MiB figure above is specifically sd-server's process usage.

Relevant verbose log excerpts

2026-11-24T18:31:38.223Z [V] version: stable-diffusion.cpp version unknown, commit ce7ef90
2026-11-24T18:32:52.508Z [V] free compute buffer: 5.57 MB(VRAM) on CUDA0
2026-11-24T18:33:06.622Z [V] free compute buffer: 775.11 MB(VRAM) on CUDA0
2026-11-24T18:33:12.835Z [V] free compute buffer: 289.13 MB(VRAM) on CUDA0
2026-11-24T18:33:12.839Z [I] computing vae decode graph completed, taking 6.21s
2026-11-24T18:35:13.178Z [V] DGL unloaded 3 cached model components

These excerpts confirm the timeout runs approximately 120 seconds after generation. They do not establish which allocations account for the remaining VRAM, so I am not assuming it is a leak.

Expected behavior / feature request if intentional

My use case is sharing this GPU with another inference service while image generation is idle. Ideally, idle unloading would bring sd-server close to its fresh-start memory footprint while keeping its HTTP server and model configuration available for the next request.

If retaining the backend allocations is intentional for --weight-residency-ttl, could an additional opt-in argument such as --context-idle-ttl <seconds> be provided? It could destroy the model/execution context and release its backend resources after the timeout, preserve the selected configuration, then recreate the context on the next generation request. Exact naming and implementation are suggestions; the goal is to release the additional ~4 GiB while accepting a cold reload on the next request. Small unavoidable process/driver overhead is fine.

The earlier fork's full-context unload worked well for this shared-GPU use case. This report follows up on #2406 and #2239 after #2385 was closed as superseded; it highlights the difference between component residency release and returning near the fresh-start VRAM footprint.

Activity

  1. nitedani commented on Nov 25, 2026

    @nitedani

    ClaudeCodex Claude Opus 5.5 (high effort) wrote this; GPT-6.2-sol (Codex, medium effort) reviewed it.

    With --weight-residency-ttl 15, our unpatched sd-server still used about 4.5 GiB of VRAM 35 seconds after the job. For us an idle sd-server has kept VRAM since master-1117, as #2401 reports. ggml intentionally keeps eligible freed CUDA buffers for reuse, up to 4 GiB per device (ggml-cuda.cu#L958-L979). That cache could account for some of the VRAM left after the residency timeout.

    On master-1127 with Z-Image Turbo on a 12 GB RTX 3080 Ti, --offload-to-cpu and --weight-staging-ttl 0, this patch took idle VRAM from 4162–4780 MiB to 814 MiB. It empties ggml's reusable cache on the CUDA devices it finds in the staging list, previously cached buffers included (clear):

    --- a/src/model_manager.cpp
    +++ b/src/model_manager.cpp
    @@ -1747,8 +1747,15 @@ void ModelManager::release_idle_gpu_staging() {
                     target_states.insert(state);
             }
         }
    +    std::unordered_set<ggml_backend_t> backends;
    +    for (const auto& block : compute_staging_blocks_)
    +        backends.insert(block->compute_backend);
         if (!target_states.empty())
             release_compute_staging_blocks(false, &target_states);
    +    // The backend's buffer cache keeps what the release freed (up to 4 GiB of VRAM) for the next job: idle, hand it back.
    +    for (ggml_backend_t backend : backends)
    +        if (backend != nullptr)
    +            sd_backend_clear_buffer_cache(backend);
     }
     
     void ModelManager::release_compute_staging_blocks(bool force,

    It doesn't reach your path, because residency expiry calls the model unload instead (async_jobs.cpp#L731-L744). With --weight-residency-ttl 15 alone, the server held 4596 MiB unpatched and 4412 MiB with it, 5 and 35 seconds after the job, and the next job returned HTTP 200 on both builds.

    We haven't tried the same clear after the residency unload, so I can't say how close to the fresh-start 128 MiB it gets, or whether --context-idle-ttl would still be needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions