stratopause-lscandClaude Opus 4.8 b7c3cc181d Show MLX Metal bar without requiring a probe node
The /macmem/stats route previously never read MLX and returned a cached
value populated only by the MacMemProbe node, so the "MLX Metal" bar stayed
hidden (display:none) until a probe ran in the graph.

Read MLX memory live in collect_stats() whenever the server is idle
(mlx_live or not _is_executing()), keeping the last good value during
execution. Reading MLX's memory counters (get_active/cache/peak_memory) is
a global per-process query that is safe from any thread — verified under
concurrent GPU compute — and gating the route read on "not executing"
ensures MLX is never touched while a prompt runs on the executor thread.

The Metal bar now appears automatically after any MLX/mflux activity.
MacMemProbe still works and is now optional (live updates during a run).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 17:27:38 +08:00

ComfyUI-MacMemoryMonitor

A real-time memory monitor for ComfyUI on Apple Silicon (MPS / unified memory).

It's the Mac analogue of kijai/ComfyUI-MemoryVisualization, but rewritten for the unified-memory model: a Mac has no separate VRAM pool — the GPU and CPU share one memory bank — so this reports unified RAM + the MPS allocator's view of it, instead of "VRAM vs RAM".

What it shows

A small floating panel (top-left by default, draggable, collapsible):

Bar Source
Unified RAM used / total psutil.virtual_memory()
MPS driver allocated, vs the recommended-max "budget" torch.mps.driver_allocated_memory() / torch.mps.recommended_max_memory()
MPS active (currently allocated tensors) torch.mps.current_allocated_memory()
MLX Metal (active + cache), vs budget — populated by the Mac Memory Probe node mlx.core.get_active_memory() + get_cache_memory()
Swap used / total psutil.swap_memory()
Meta line: CPU %, ComfyUI process RSS, MLX cache, MPS budget psutil + torch + mlx
Loaded models list with sizes + device comfy.model_management.current_loaded_models

Why the MLX bar matters: MLX-based nodes (e.g. mflux) allocate Metal memory directly, bypassing PyTorch — so torch.mps reports it as ~0 and ComfyUI's model manager shows "no models loaded" even while gigabytes are in use. The MLX Metal bar surfaces that hidden memory, and the MLX cache figure is the part that accumulates across repeated runs (a common cause of out-of-memory crashes on unified-memory Macs).

Thread-safety note: MLX uses per-thread GPU streams, so its memory functions must only be called from the ComfyUI execution thread — calling them from the web-server thread can throw There is no Stream(gpu, 0) in current thread, an uncatchable abort. So MLX figures are captured by the Mac Memory Probe node (which runs on the execution thread) and the panel shows the most recent snapshot; the HTTP route never calls MLX directly. Add a probe node to your graph to populate the MLX bar.

During a run the title shows a green ● and a yellow peak marker tracks the high-water mark for Unified RAM, MPS-driver, and MLX memory (reset at each run start).

Two ways to use it

  1. Floating panel — appears automatically. Toggle it from the ComfyUI settings (⚙) → "Mac Memory Monitor: show panel", or the ✕ on the panel.
  2. Mac Memory Probe node (utils/memory category) — a pass-through you can wire inline on any connection (image, latent, model, …). When it executes it prints a memory snapshot to the console and shows it on the node, so you can see usage at an exact point in the graph (e.g. right after the sampler, before VAE decode). It passes its input straight through, plus a report string output.

How it works

  • __init__.py registers an aiohttp GET route /macmem/stats on ComfyUI's server that returns all the figures above as JSON, and the MacMemProbe node.
  • web/mac_mem_monitor.js registers a ComfyUI extension that polls that route every 500 ms and draws the DOM panel; it listens to execution_start / execution_success API events to drive the run indicator and peak reset.

No canvas, no themes, no CUDA/NVML dependencies — intentionally minimal.

Example workflow

example_workflows/mflux_anymodel_memory_monitor.json — a text-to-image run using the mflux Model Loader (MLX) → mflux Sampler (MLX) nodes (from comfyui-mflux-anymodel) with a Mac Memory Probe wired onto the image output, feeding a Preview. It includes a How-to-use note node on the canvas.

Load it via ComfyUI's Workflow → Open (or drag the .json onto the canvas), make sure the floating panel is visible, then Queue Prompt and watch memory climb during the run. Requires the comfyui-mflux-anymodel nodes to be installed (they are, in this ComfyUI).

Install

cd ComfyUI/custom_nodes
git clone https://github.com/stratopause-lsc/ComfyUI-MacMemoryMonitor

Then restart ComfyUI. No extra dependencies beyond what ComfyUI already ships (torch, psutil).

  • Browser: hard-refresh with ⌘⇧R after installing/updating.
  • ComfyUI Desktop app: the webview caches extension files aggressively — after installing or updating, do a hard reload (⌘⇧R) or fully quit and reopen, or the panel can render with stale/unstyled assets.

Notes / limits

  • torch.mps.* figures reflect PyTorch's MPS allocator only. Other GPU users on the system (other apps, other processes) aren't attributed — for the true whole-machine picture use the Unified RAM bar, which is system-wide.
  • macOS has no per-process GPU-memory API as clean as NVML; there's deliberately no "GPU utilization %" or temperature/power readout like the CUDA original.

Credits

Architecture inspired by kijai/ComfyUI-MemoryVisualization (the NVIDIA/VRAM equivalent). This project shares no code with it — the entire Python backend and JavaScript frontend were written from scratch for Apple Silicon / MPS unified memory. Thanks to kijai for the original idea.

License

MIT — see LICENSE.

S
Description
No description provided
Readme MIT
46 KiB
Languages
JavaScript 47.4%
Python 43.1%
CSS 9.5%