ComfyUI icon indicating copy to clipboard operation
ComfyUI copied to clipboard

total RAM reported wrong in Docker

Open btaens opened this issue 1 year ago • 2 comments

Expected Behavior

Comfyui should report correct Total RAM based on Docker settings.

Actual Behavior

ComfyUI reports wrong total RAM, due to it using psutil, which will report the host RAM, and ignores Docker --memory flag.

Steps to Reproduce

  • Run ComfyUI in Docker.
  • Set --memory flag to limit memory to a lower amount.
  • When starting ComfyUI, it will report the host's total RAM, not the amount set on the Container.
  • This also causes smart memory management to fail, as it thinks it has more RAM available than it does, and the OS kills the process when it goes above it.

Debug Logs

Not applicable

Other

This issue is due to ComfyUI using psutil.virtual_memory() to check available memory. PSUtil however uses /proc filesystem to retrieve this info, which reports the host's stats, not the container's.

This could either be solved by some flag use cgroups to retrieve that information, or a simpler flag that limits available memory to a set amount.

btaens avatar Nov 15 '24 13:11 btaens

I have a temporary solution using a patch

use patch because some custom nodes are also using psutil.virtual_memory, for example crytools.

  • create /root/ComfyUI/patch/fake_psutil.py
from unittest.mock import patch
import psutil

def fake_virtual_memory():
    class FakeMemory:
        total = int(open("/sys/fs/cgroup/memory.max").read().strip())
        used = int(open("/sys/fs/cgroup/memory.current").read().strip())
        available = total - used
        percent = (used / total) * 100 if total > 0 else 0

    return FakeMemory()

patch("psutil.virtual_memory", fake_virtual_memory).start()

print("***************** Fake psutil loaded")

  • add import to /root/ComfyUI/main.py first line
import fake_psutil
  • use export in start command
export PYTHONPATH=/root/ComfyUI/patch && python /root/ComfyUI/main.py

springjk avatar Apr 20 '25 12:04 springjk

I have released an extension that can solve this problem non-invasively after installation

search Psutil Container Memory Patch in ComfyUI-Manager

https://github.com/springjk/ComfyUI-Psutil-Container-Memory-Patch

springjk avatar Apr 23 '25 15:04 springjk