Implementation:AUTOMATIC1111 Stable diffusion webui Memory Monitor
| Knowledge Sources | |
|---|---|
| Domains | Memory_Management, GPU_Monitoring |
| Last Updated | 2025-05-15 00:00 GMT |
Overview
Provides a background daemon thread that continuously monitors CUDA GPU memory usage during image generation, tracking peak active, reserved, and system memory statistics.
Description
The Memory Monitor module implements the MemUsageMonitor class, a daemon thread that polls CUDA memory statistics at a configurable rate. When activated via the monitor() method, it resets PyTorch's peak memory statistics and begins tracking the minimum free system memory in a polling loop. The read() method captures current memory metrics from both torch.cuda.mem_get_info (free and total system VRAM) and torch.cuda.memory_stats (active current, active peak, reserved current, reserved peak). The stop() method halts polling and returns the collected statistics. The monitor gracefully handles non-CUDA environments (e.g., AMD GPUs) by disabling itself if CUDA memory functions raise exceptions during initialization. Memory data is stored in a defaultdict and includes: free, total, active, active_peak, reserved, reserved_peak, min_free, and system_peak.
Usage
Use this module to collect GPU memory usage statistics during image generation tasks, which are then displayed in the WebUI as performance metrics (active peak, reserved peak, system peak) alongside generation time.
Code Reference
Source Location
- Repository: AUTOMATIC1111_Stable_diffusion_webui
- File: modules/memmon.py
- Lines: 1-92
Signature
class MemUsageMonitor(threading.Thread):
def __init__(self, name: str, device: torch.device, opts) -> None
def cuda_mem_get_info(self) -> tuple[int, int]
def run(self) -> None
def dump_debug(self) -> None
def monitor(self) -> None
def read(self) -> dict
def stop(self) -> dict
Import
from modules.memmon import MemUsageMonitor
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | str | Yes | Thread name for identification |
| device | torch.device | Yes | The CUDA device to monitor |
| opts | object | Yes | Options object with memmon_poll_rate attribute (polls per second) |
Outputs
| Name | Type | Description |
|---|---|---|
| data | dict | Dictionary containing memory statistics: free, total, active, active_peak, reserved, reserved_peak, min_free, system_peak (all in bytes) |
Usage Examples
import torch
from modules.memmon import MemUsageMonitor
device = torch.device("cuda:0")
monitor = MemUsageMonitor("gpu_monitor", device, opts)
monitor.start() # Start the daemon thread
# Before a generation task
monitor.monitor()
# ... perform GPU operations ...
# After the task, collect stats
stats = monitor.stop()
active_peak_mb = stats['active_peak'] / (1024 * 1024)
print(f"Peak active VRAM: {active_peak_mb:.1f} MB")