Monitor GPU Memory Usage of FFmpeg NVENC

Monitoring GPU memory (VRAM) during an FFmpeg NVENC transcode is essential for optimizing hardware resource allocation and preventing out-of-memory (OOM) errors. This article explains how to track Nvidia GPU memory usage in real-time using standard command-line tools like nvidia-smi, interactive monitors like nvtop, and programmatic scripts for automated workflows.

Method 1: Using nvidia-smi (Command Line)

The most direct way to monitor GPU memory is using the Nvidia System Management Interface (nvidia-smi), which comes pre-installed with Nvidia proprietary drivers.

Quick Snapshot

To get a one-time snapshot of your GPU memory usage while FFmpeg is running, execute:

nvidia-smi

This display shows the total VRAM, currently used VRAM, and a list of active processes (where you should see ffmpeg listed alongside its memory footprint).

Real-Time Loop

To monitor the memory dynamically in real-time (updating every second), use the watch command:

watch -n 1 nvidia-smi

Loggable CSV Stream

If you want to log the memory usage data to a file or stream it directly to your terminal console, you can query specific memory metrics:

nvidia-smi --query-gpu=timestamp,memory.total,memory.used,memory.free --format=csv -l 1

Method 2: Using nvtop (Visual Interface)

nvtop (Nvidia TOP) is an interactive, visual task monitor for GPUs, similar to the standard system monitor htop. It displays real-time graphs for memory and GPU utilization, alongside a list of running processes and their exact VRAM usage.

Installation

To install nvtop on Debian/Ubuntu-based systems:

sudo apt update
sudo apt install nvtop

For Red Hat/CentOS systems:

sudo dnf install epel-release
sudo dnf install nvtop

Running nvtop

Launch the interface by typing:

nvtop

This will open a color-coded terminal GUI where you can watch the VRAM consumption spike and stabilize as FFmpeg processes your NVENC transcode.

Method 3: Programmatic Monitoring with Python

If you need to log GPU memory usage programmatically during an automated FFmpeg batch transcode, you can use Python with the GPUtil library.

Installation

First, install the library:

pip install GPUtil

Python Script

Run the following script alongside your FFmpeg process to print or log the exact memory usage every second:

import GPUtil
import time

try:
    while True:
        gpus = GPUtil.getGPUs()
        for gpu in gpus:
            print(f"GPU: {gpu.name} | VRAM Used: {gpu.memoryUsed}MB / {gpu.memoryTotal}MB ({gpu.memoryUtil*100:.1f}%)")
        time.sleep(1)
except KeyboardInterrupt:
    print("Monitoring stopped.")