#26342·ultralytics

Repeated Annotator usage with `pil=True` results in unsustainable growth in memory usage

Author: StijnvanderLippeCreated Sep 25, 2026Updated Sep 25, 2026
Labelsbugtriage

Search before asking

  • I have searched the Ultralytics YOLO issues and found no similar bug report.

Ultralytics YOLO Component

Other

Bug

When creating an annotated video from inference results I repeatedly use the ultralytics.utils.plotting.Annotator class on each frame. When using the Annotator class with pil=True, this results in the memory usage blowing up. This is not the case when pil=False and the Annotator uses OpenCV for creating the annotated frame.

If you do use pil=True, then calling gc.collect() every so often does stabilise memory usage, but it's not clear to the user that is required. Furthermore, this slows down the code exeuction.

Output of the MRE with pil=True, no gc.collect():

0 298.2 MB
100 1148.7 MB
200 1978.6 MB
300 2802.3 MB
400 3632.0 MB
500 4461.6 MB

Output of the MRE with pil=True, with gc.collect():

0 298.6 MB
100 347.8 MB
200 1101.3 MB
300 1117.1 MB
400 1099.5 MB
500 1115.1 MB

Output of the MRE with pil=False, no gc.collect():

0 278.4 MB
100 284.4 MB
200 284.4 MB
300 284.4 MB
400 284.4 MB
500 284.4 MB

Environment

Ultralytics 8.4.155  Python-3.14.2 torch-2.14.0+cpu CPU (Intel Xeon CPU E5-2673 v4 @ 2.30GHz)
Setup complete ✅ (8 CPUs, 31.3 GB RAM, 98.5/118.1 GB disk)

OS                     Linux-6.8.0-1044-azure-x86_64-with-glibc2.35
Environment            Linux
Python                 3.14.2
Install                git
RAM                    31.35 GB
Disk                   98.5/118.1 GB
CPU                    Intel Xeon CPU E5-2673 v4 @ 2.30GHz
CPU count              8
GPU                    None
GPU count              None
CUDA                   None

cloudpickle            ✅ 3.1.2>=3.1.1
filelock               ✅ 3.32.5>=3.16.1
numpy                  ✅ 2.5.3>=1.23.0; platform_system != "Darwin"
numpy                  ✅ 2.5.3!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,!=2.3.1,!=2.3.2,!=2.3.3,!=2.3.4,>=1.23.0; platform_system == "Darwin"
matplotlib             ✅ 3.11.1>=3.3.0
opencv-python          ✅ 5.0.0.93!=4.13.0.90,>=4.7.0
pillow                 ✅ 12.3.0>=7.1.2
pyyaml                 ✅ 6.0.3>=5.3.1
requests               ✅ 2.34.2>=2.23.0
torch                  ✅ 2.14.0+cpu>=1.8.0
torch                  ✅ 2.14.0+cpu!=2.4.0,>=1.8.0; sys_platform == "win32"
torchvision            ✅ 0.29.0+cpu>=0.9.0
psutil                 ✅ 7.2.2>=5.8.0
polars                 ✅ 1.44.1>=0.20.0
nvidia-ml-py           ✅ 13.610.43>=12.0.0
ultralytics-thop       ✅ 2.1.6>=2.1.6
ultralytics-platform   ✅ 0.1.48>=0.1.45; python_version >= "3.11"

Minimal Reproducible Example

Example loop over frames, imagine the frames coming from OpenCV reading a video.

python
import gc
import os

import numpy as np
import psutil
from ultralytics.utils.plotting import Annotator, colors

process = psutil.Process(os.getpid())

frame = np.zeros((1080, 1920, 3), dtype=np.uint8)
box = (100, 100, 500, 500)
label = "test (99%)"

for i in range(10_000):
    im = frame.copy()

    annotator = Annotator(im, pil=True)
    annotator.box_label(box=box, label=label, color=colors(0))
    annotated = annotator.result()

    if i % 100 == 0:
        # Calling gc.collect() does stabilise memory usage, but it's not obvious a user should have to do so
        # gc.collect()
        rss_mb = process.memory_info().rss / 1024 / 1024
        print(i, f"{rss_mb:.1f} MB")

Additional

Are there better ways to create an annotated video than the code snippet above?

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!