Skip to content

Memory leak in cv::subtract (4.11.0) #27171

@alexanderkomarovincode3

Description

System Information

ubuntu:22.04
opencv-python-headless:4.11.0.86
python3.10

Detailed description

memory usage steadily increases over time.
I tested it on Ubuntu, MacOS, Amazon Linux2, and it showed the same behaviour.

python3.10 test_memory_leak_cv.py 
Iteration 100: Current memory usage is 0.767082MB; Peak was 7.060255MB
Iteration 200: Current memory usage is 0.774338MB; Peak was 7.066686MB
Iteration 300: Current memory usage is 0.781566MB; Peak was 7.073914MB
Iteration 400: Current memory usage is 0.788766MB; Peak was 7.081114MB
Iteration 500: Current memory usage is 0.795966MB; Peak was 7.088314MB
Iteration 600: Current memory usage is 0.803618MB; Peak was 7.095966MB
Iteration 700: Current memory usage is 0.810818MB; Peak was 7.103166MB
Iteration 800: Current memory usage is 0.818018MB; Peak was 7.110366MB
Iteration 900: Current memory usage is 0.825218MB; Peak was 7.117566MB
Iteration 1000: Current memory usage is 0.832418MB; Peak was 7.124766MB
Iteration 1100: Current memory usage is 0.839618MB; Peak was 7.131966MB
Iteration 1200: Current memory usage is 0.846818MB; Peak was 7.139166MB
Iteration 1300: Current memory usage is 0.854018MB; Peak was 7.146366MB
Iteration 1400: Current memory usage is 0.861218MB; Peak was 7.153566MB
Iteration 1500: Current memory usage is 0.868418MB; Peak was 7.160766MB
Iteration 1600: Current memory usage is 0.875618MB; Peak was 7.167966MB
Iteration 1700: Current memory usage is 0.882818MB; Peak was 7.175166MB
Iteration 1800: Current memory usage is 0.890018MB; Peak was 7.182366MB
Iteration 1900: Current memory usage is 0.897218MB; Peak was 7.189566MB
Iteration 2000: Current memory usage is 0.904418MB; Peak was 7.196766MB
Iteration 2100: Current memory usage is 0.911618MB; Peak was 7.203966MB
Iteration 2200: Current memory usage is 0.918818MB; Peak was 7.211166MB
Iteration 2300: Current memory usage is 0.926018MB; Peak was 7.218366MB

Steps to reproduce

import cv2
import numpy as np
import gc
import tracemalloc

def test_subtract_memory_leak(iterations=5000):
    """
    Tests for memory leaks when using cv2.subtract in a loop.
    """

    tracemalloc.start()

    for i in range(iterations):
        img1 = np.random.randint(0, 256, (512, 512, 3), dtype=np.uint8).astype(np.float32)
        img2 = np.random.randint(0, 256, (3), dtype=np.uint8).astype(np.float32)

        result = cv2.subtract(img1, img2) # main operation for testing

        # Explicitly delete variables to help garbage collection.
        del img1, img2, result
        gc.collect() # Force garbage collection

        if (i+1) % 100 == 0:
            current, peak = tracemalloc.get_traced_memory()
            print(f"Iteration {i+1}: Current memory usage is {current / 10**6}MB; Peak was {peak / 10**6}MB")

    tracemalloc.stop()

test_subtract_memory_leak()


Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcategory: coreneeds investigationCollect and attach more details (build flags, stacktraces, input dumps, etc)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions