Skip to content

[Bug] VRAM/Memory leak when repeatedly running Qwen3-VL GGUF models on Windows and macOS #58

Closed

Description

@handycn

Prerequisites
I am running the latest code (tested with v0.3.23 and v0.3.24).

I carefully followed the README.md in JamePeng's repository.

I searched existing issues and found no duplicate.

Expected Behavior
After each inference run using Qwen3-VL GGUF models via llama-cpp-python (JamePeng's fork) in ComfyUI, all GPU memory (Windows) or unified memory (macOS) allocated by the library should be fully released, returning to baseline levels without accumulation over multiple runs.

Current Behavior
On both Windows and macOS, memory usage increases by a fixed amount (~1GB) after each inference run. This memory is never released and accumulates until an out-of-memory (OOM) crash occurs. The issue is reproducible in both versions 0.3.23 and 0.3.24.

Important version note:

v0.3.23: Normal inference speed (~20-30 tok/s), but leaks memory.

v0.3.24: Severe performance regression (>7x slower, ~4 tok/s), and also leaks memory. The slowdown and the leak appear to be independent issues.

Environment
Hardware:

Windows: NVIDIA GeForce RTX 5070 Ti (16GB VRAM)

macOS: Apple M3 (16GB unified memory)

OS:

Windows 11

macOS 26.2

Software:

Python: 3.13.11

llama-cpp-python: 0.3.23 and 0.3.24

ComfyUI: v0.11.1 (issue is backend-related, front-end independent)

Models Tested
Qwen3-VL-30B-A3B-Instruct-IQ4_XS.gguf (~16.4GB)

Qwen3-VL-4B-Instruct-Q6_K.gguf (~3.3GB)

Steps to Reproduce
Install llama-cpp-python (JamePeng's fork) with CUDA support on Windows or via pip on macOS.

Download any Qwen3-VL GGUF model (e.g., the 4B model for macOS testing).

Use any ComfyUI front-end node that supports llama-cpp-python (e.g., ComfyUI-QwenVL or ComfyUI-llama-cpp).

Run a single inference (e.g., image/video captioning) and record baseline memory.

Run 5 consecutive inferences without restarting ComfyUI.

Monitor memory:

Windows: nvidia-smi -l 1

macOS: Activity Monitor (watch Python process memory)

Evidence (macOS with 4B model)
Stage Python Process Memory (GB)
After ComfyUI startup 0.65
After 1st run 1.4
After 2nd run 2.3
After 3rd run 3.1
After 4th run 4.3
After 5th run 4.7
On Windows, nvidia-smi shows identical stepwise growth (screenshots available upon request).

Additional Notes
The issue occurs with different front-end nodes, confirming the leak is in the backend (llama-cpp-python) itself.

The problem is specific to GGUF models; other model formats (e.g., diffusers) do not exhibit this leak.

Cross-platform reproducibility (Windows + macOS) strongly suggests a fundamental issue in the framework, not a driver quirk.

Activity

  1. JamePeng commented on Feb 14, 2026

    @JamePeng
    Owner

    I recently refactored the code in the sample section to better suit the new execution method in llama.cpp. It's only a preliminary implementation so far, and we'll continue to optimize the runtime efficiency. Currently, the code in the sample section still has issues with repeatedly creating dictionary array variables, which will also be optimized. Other optimizations are also under testing.

  2. JamePeng commented on Feb 14, 2026

    @JamePeng
    Owner

    You can try the new patch for sample workflow:5ef874c

    Three simple tests in my macmini m4: Who are you? Please introduce yourself. Please introduce this paper.pdf.
    old sample workflow,sample used 71s:
    Image

    new sample workflow, sample used 55s:
    Image

  3. handycn commented on Feb 15, 2026

    @handycn
    Author

    Thank you very much for your active optimization work. I really appreciate it.
    Since it's currently a holiday in China and I don't have my Mac with me, I'll run the tests on Windows later and share the results with you as soon as possible.

  4. handycn commented on Feb 17, 2026

    @handycn
    Author

    Hi @JamePeng,

    Thank you for the new release v0.3.25. I have completed comparative tests on my Windows machine using the same Qwen3-VL GGUF model and workflow. Below are the results for v0.3.23, v0.3.24, and v0.3.25.

    Test Environment
    OS: Windows 11 (build 26200)

    CPU: AMD Ryzen 7 9700X (16 cores)

    RAM: 48 GB

    GPU: NVIDIA GeForce RTX 5070 Ti (16 GB VRAM)

    Driver: 591.44

    CUDA: 13.1

    Python: 3.13.11

    ComfyUI: v0.11.1 (frontend 1.37.11)

    Frontend Plugin: 1038lab/ComfyUI-QwenVL

    Model: Huihui-Qwen3-VL-8B-Instruct-abliterated.Q6_K.gguf

    Input: Same image and prompt for all runs

    Seeds: Randomized (control_after_generate = randomize) to ensure actual recomputation each time

    Metrics: VRAM usage (MiB) and inference speed (tokens/s) recorded after each run, starting from ComfyUI launch.

    Image

    Observations
    Memory leak persists: All three versions show a steady increase in VRAM after each run, with total growth of ~6.9–7.4 GB after 5 runs. The leak does not appear to be fixed in v0.3.25.

    Speed regression in v0.3.25: Compared to v0.3.23, v0.3.25 is slightly slower (average ~66 vs. ~69 tok/s), though it is more stable than v0.3.24, which had a sharp drop at run 5.

    Request
    Could you please take another look at the memory leak issue? The data suggests that the underlying problem (possibly related to vision tower resources or CUDA context not being freed) is still present. I'm happy to run additional tests or provide more details if needed.

    Thanks again for your continuous efforts!

    Additional context:
    I previously suspected that the leak might be caused by the ComfyUI-QwenVL plugin, so I switched to another plugin — ComfyUI-llama-cpp_vlm (by lihaoyun6, which also uses JamePeng's llama-cpp-python backend) — and tested again. The same memory leak pattern appeared, which led me to conclude that the issue is indeed in the backend (llama-cpp-python), not in any specific frontend plugin. The data above was collected using the 1038lab/ComfyUI-QwenVL plugin, but both plugins show identical leak behavior.

  5. JamePeng commented on Feb 17, 2026

    @JamePeng
    Owner

    Based on your tests, the optimization of the sample processing flow is effective; memory usage is significantly less than in version 0.3.23. I have another optimized version that will be updated in the next few days, and you can try it then. As for the increased VRAM usage you mentioned, I'll take a look at that later.

  6. handycn commented on Feb 17, 2026

    @handycn
    Author

    Thank you for the update! I'm glad the optimizations are showing some improvement. I'll keep an eye out for the next version and run another test when it's released. In the meantime, I've switched to Ollama as a temporary workaround to avoid the memory leak. Looking forward to your new release!

  7. JamePeng commented on Feb 19, 2026

    @JamePeng
    Owner

    Version 0.3.26 has been released. You can wait until it's compiled before comparing and testing it.

  8. handycn commented on Feb 21, 2026

    @handycn
    Author

    Hi @JamePeng,

    First of all, I want to sincerely thank you for your continuous efforts and the quick releases of v0.3.25 and v0.3.26! 🙏 I really appreciate how actively you’ve been optimizing and responding to feedback. The performance improvements in the sample workflow are clearly visible, and the speed regression in v0.3.24 has been resolved in the latest version — great work!

    I’ve just finished some additional tests on my side, and I’d like to share the results with you, hoping they might be useful for your ongoing optimization.

    Test Setup

    • Environment: macOS (Apple M3) + Python 3.12.8, using your pre‑built wheel llama_cpp_python-0.3.26-cp312-cp312-macosx_11_0_arm64.whl (Metal enabled).

    • Model: Qwen3-VL-4B-Instruct-abliterated-v2.Q6_K.gguf with its corresponding mmproj file.

    • Method: A minimal Python script that loads the model once and runs 10 consecutive inference passes with the same image and prompt, recording RSS memory after each step.

    Memory Measurements (RSS in MB)

    Step | RSS (MB) -- | -- start | 73 after load | 3562 iter 1 after | 4535 iter 2 after | 4543 iter 3 after | 4546 iter 4 after | 4550 iter 5 after | 4550 iter 6 after | 4553 iter 7 after | 4553 iter 8 after | 4553 iter 9 after | 4556 iter 10 after | 4557 after del + GC | 1104

    Observations

    • The first inference increases memory by ~970 MB (expected: image embeddings + KV cache).

    • Subsequent inferences show only tiny growth (~1 MB per step, likely normal allocation).

    • After deleting the Llama instance and forcing garbage collection, RSS drops to ~1.1 GB (the remaining memory probably belongs to Metal’s reserved GPU cache or Python process overhead).

    • No cumulative, unbounded growth – the total increase from iteration 2 to iteration 10 is only about 14 MB, which is perfectly normal.

    This confirms that llama-cpp-python itself (especially v0.3.26) does not leak memory when used in a plain Python script. The leak I observed earlier in ComfyUI has now been isolated to the 1038lab/ComfyUI-QwenVL plugin; I’ll follow up with its maintainer separately.

    Thank you again for your fantastic work on this project and for the quick iterations. It’s a pleasure to see how responsive and dedicated you are! I’ll keep testing future versions and will let you know if I find anything else.

    Best regards,
    handycn


    P.S. If you ever need more detailed logs or want me to run any specific test, just let me know. I’m happy to help.

  9. JamePeng commented on Feb 21, 2026

    @JamePeng
    Owner

    OK~Thank you very much for your testing feedback; it is very helpful for me in maintaining and improving the project.

    After releasing version 0.3.26, I still fixed a few potentially unreleased areas. You can try them again in the next version.

  10. handycn commented on Feb 21, 2026

    @handycn
    Author

    Hi @JamePeng,
    After your suggestion to try the next version, I wanted to double‑check the memory situation more thoroughly. My previous test (10 consecutive inferences with the model loaded once) showed only ~14 MB growth over 10 runs, which seemed great. However, in my real use case with ComfyUI, I unload the model after each inference (i.e., each workflow run reloads the model from scratch). That’s a very different pattern, and I realised that my earlier test might have missed leaks that only occur during loading/unloading.

    So I ran a new test that simulates exactly that: load the model → run one inference → unload the model, and repeat 5 times. I used a standalone Python script (attached) instead of ComfyUI to rule out any plugin‑specific issues – this way we know any leak is in the backend itself.

    Here are the results (RSS after each unload):

    Cycle RSS after unload (MB) Delta
    1 1061.67 —
    2 1945.47 +884
    3 2791.48 +846
    4 3475.23 +684
    5 4328.22 +853

    Image

    Observations:

    Each load/unload cycle leaves behind about 800 MB of memory that is never freed.

    After 5 cycles, the process has accumulated an extra 3.3 GB.

    This perfectly matches the behaviour I saw in ComfyUI (memory growing steadily until OOM).

    Why this matters:

    The leak only shows up when the model is reloaded multiple times, not when it’s kept loaded continuously.

    This is exactly the pattern used by many ComfyUI workflows that don’t keep the model loaded.

    Because I used a plain Python script, we can be sure the leak is in llama-cpp-python / llama.cpp, not in any ComfyUI plugin.

    I’ve attached the test script and the full console output. If you need any more details or want me to run additional tests, just let me know.

    Thank you again for your fantastic work – this project is incredibly valuable, and I’m happy to help make it even better!

    Best regards,
    handycn

  11. JamePeng commented on Feb 21, 2026

    @JamePeng
    Owner

    It's possible that some exit implementations in the original llama-cpp-python version of llama might not fully release the data until Python completely finishes. I think we should modify the model exit and release mechanism later.

  12. JamePeng commented on Feb 22, 2026

    @JamePeng
    Owner

    Hello @handycn
    The biggest problem I've identified is that the clip model isn't being released after loading. The amount of memory increase you provided is exactly the same as the size of the qwen3-vl clip model.
    You can try version 0.3.27 again. I have added a lot of fixes and enhanced the exit and nulling mechanisms of several classes to improve the efficiency of GC collection.

  13. handycn commented on Feb 23, 2026

    @handycn
    Author

    Hi @JamePeng,

    I just wanted to express my sincere thanks for your rapid response and the excellent fixes in v0.3.27! 🙏 Your work on resolving the VRAM leak is really impressive and much appreciated.

    I’ve completed two rounds of testing with the new version, and I’d like to share the results with you.


    1. Text‑only model (0.3.26)

    • Model: Qwen3-8B-Hivemind-Inst-Hrtic-Ablit-Uncensored-Q6_K-imat.gguf

    • Test: Repeated load/inference/unload cycles (5 times)

    • Results:

    Cycle | RSS after unload (MB) -- | -- 1 | 141.69 2 | 173.80 3 | 177.91 4 | 194.69 5 | 195.00
    • Observation:

      • The main leak (clip model) is completely fixed! Previously each cycle left ~800 MB behind; now it stays below 320 MB.

      • There is still a small residual increase of ~88 MB over 5 cycles, similar to the text‑only model. This is likely Metal‑related overhead and is negligible for everyday use.

    3. Conclusion

    • v0.3.27 has successfully eliminated the severe OOM‑causing leak in multimodal models.

    • The remaining tiny growth (~50–90 MB after several cycles) is perfectly acceptable – users can now run repeated load/unload workflows without worry.

    Thank you again for your dedication and for making this library so reliable. I’m happy to run any additional tests if needed.

    Best regards,
    handycn

  14. JamePeng commented on Feb 23, 2026

    @JamePeng
    Owner

    Nice 😄

  15. handycn commented on Feb 23, 2026

    @handycn
    Author

    Hi @JamePeng,

    I’m absolutely thrilled to share that the memory leak is completely gone in my ComfyUI workflow with v0.3.27! 🎉🎉🎉

    After months of struggling with this issue, your fix has finally resolved it. I just ran 5 consecutive inference runs in ComfyUI (using a Qwen3-VL GGUF model) and monitored the memory. Here’s what I saw:

    Before (0.3.26): Each run left ~800 MB behind, quickly leading to OOM after a few runs.

    After (0.3.27): Memory stabilizes around 840 MB after each run, with only tiny fluctuations. No accumulation at all!

    Image

    I’ve attached a screenshot of the terminal output for reference. This is a massive improvement – it’s now perfectly usable for my daily work.

    Thank you so much for your dedication and for the incredibly fast turnaround. This fix makes a huge difference for me and for everyone using multimodal models with llama-cpp-python. You’re awesome! 🙌

    Best regards,
    handycn

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