-
-
Notifications
You must be signed in to change notification settings - Fork 56.5k
opencv-python Memory leak caused by failed cvtColor() calls #23633
Description
System Information
OpenCV python version: 4.7.0.72
Operating System: Ubuntu 18.04
Python version: 3.8.0
Detailed description
In my application frames are received from multiple sensors and processed as fast as possible. The processing step is designed in a way, that some exceptions can occur in small number and are ignored in order to keep the system running.
Multiple mistakes resulted that following case:
Some frames got passed to process using wrong dtype and caused an error at cvtColor() function. The exception got logged but ignored, the frame was dropped and the process kept running. And I noticed unusual memory usage in the long run.
Steps to reproduce
Sample code to present the failed conversion:
import cv2
import numpy as np
if __name__ == "__main__":
frame1 = np.zeros((480, 640, 3), dtype=np.uint8)
gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
print("Frame1 succesfully converted\n")
frame2 = np.zeros((480, 640, 3), dtype=np.float64)
gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
print("Frame2 succesfully converted\n")Produces the following output:
Frame1 succesfully converted
Traceback (most recent call last):
File "cv_memory_leak_test.py", line 10, in <module>
gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.7.0) /io/opencv/modules/imgproc/src/color.simd_helpers.hpp:94: error: (-2:Unspecified error) in function 'cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3, 4>; VDcn = cv::impl::{anonymous}::Set<1>; VDepth = cv::impl::{anonymous}::Set<0, 2, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = cv::impl::<unnamed>::NONE; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Unsupported depth of input image:
> 'VDepth::contains(depth)'
> where
> 'depth' is 6 (CV_64F)
Sample code to reproduce noticeable memory usage:
This code simulates what would happen if the conversion would fail a lot of times. In this code memory usage is measured by using a terminal command, if you are not using Ubuntu, you might want to use a different method to track memory usage.
Note that the memory does get cleaned up, but just after the program is terminated.
import os
import cv2
import numpy as np
if __name__ == "__main__":
# frame = np.zeros((480, 640, 3), dtype=np.uint8) # No memory leaks, successful conversion
frame = np.zeros((480, 640, 3), dtype=np.float64) # Memory leak, failed conversion
N = 1_000_000 # N = 1_000_000 will result (~1.3 GB reserved memory)
for i in range(N):
# Measure memory usage
if (i % (N//100) == 0):
memory_usage = os.popen('free -h').readlines()[1].split()
print(("i = %06d | Memory used %s from %s")%(i, memory_usage[2], memory_usage[1]))
# Attempt conversion
try:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
except Exception as e:
pass # For the sake of simplicity I pass here, the exception did get logged in the actual applicationPossible output:
i = 000000 | Memory used 4,7G from 15G
i = 100000 | Memory used 4,8G from 15G
i = 200000 | Memory used 5,0G from 15G
i = 300000 | Memory used 5,1G from 15G
i = 400000 | Memory used 5,3G from 15G
i = 500000 | Memory used 5,4G from 15G
i = 600000 | Memory used 5,5G from 15G
i = 700000 | Memory used 5,7G from 15G
i = 800000 | Memory used 5,8G from 15G
i = 900000 | Memory used 6,0G from 15G
I checked some memory leak related issues in the repo, but not all of them. Please tag, if some issues relate.
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)