Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] VideoCapture does not release unmanaged resources on dispose - Memory Leak. #900

Open
adsgreen opened this issue Nov 23, 2023 · 3 comments
Assignees

Comments

@adsgreen
Copy link

adsgreen commented Nov 23, 2023

Describe the bug
When opening and connecting to a webcam device using the VideoCapture all works fine until you need to stop using the camera and dispose of the VideoCapture object.
The dispose runs fine and the managed code is removed from the heap however the private bytes remain.
If a new instance of VideoCapture device is used then the private bytes of the process increases consistently (by about 20mb or so each time).
Video Capture Disposal is the only method to stop the exclusive lock on the webcam.

** OS / Platform **
Windows 11

** .Net version **
.Net Core 7 / 8

CPU Architecture
x86_64

** Emgu CV package used**
nuget package
Emgu.CV 4.8.1.5350
Emgu.runtime.windows.msvc.rt.x64 19.37.32825

To Reproduce

using Emgu.CV;
int i = 0;
while (true)
{
    using VideoCapture _webcam = new VideoCapture(0);
    i++;
    Console.WriteLine($"Execution {i}");

    await Task.Delay(1000);    
}

By execution 150 the process memory (as reported by visual studio) is circa 2gb.

Expected behavior
Memory usage to remain reasonably constant when connecting and disconnecting from webcam.

@aloksharma1
Copy link

i can confirm this behavior still exists, when using same code on emgucv & opencvsharp for camera access emgucv used around 8gb-10gb memory before gc hit while opencvsharp stays at 100-200mb for my project. I am using avalonia as frontend with dotnet 8.

@emgucv
Copy link
Owner

emgucv commented Apr 19, 2024

The leak happens in Open CV C++ and can be reproduced using Open CV Python. It will need to be fixed on the Open CV side.

>>> import cv2
>>> cv2.__version__
'4.9.0'
>>> import psutil
>>> process = psutil.Process()
>>> for i in range(20):
...     cap = cv2.VideoCapture(0)
...     cap.release()
...     print("Step {0} memory usage: {1} bytes".format(i, process.memory_info().rss))
...
Step 0 memory usage: 95404032 bytes
Step 1 memory usage: 104742912 bytes
Step 2 memory usage: 114249728 bytes
Step 3 memory usage: 117182464 bytes
Step 4 memory usage: 126382080 bytes
Step 5 memory usage: 133971968 bytes
Step 6 memory usage: 143253504 bytes
Step 7 memory usage: 152346624 bytes
Step 8 memory usage: 162619392 bytes
Step 9 memory usage: 174252032 bytes
Step 10 memory usage: 185409536 bytes
Step 11 memory usage: 191909888 bytes
Step 12 memory usage: 199286784 bytes
Step 13 memory usage: 202575872 bytes
Step 14 memory usage: 212451328 bytes
Step 15 memory usage: 221736960 bytes
Step 16 memory usage: 223965184 bytes
Step 17 memory usage: 225353728 bytes
Step 18 memory usage: 228392960 bytes
Step 19 memory usage: 238239744 bytes
>>>

@emgucv
Copy link
Owner

emgucv commented Apr 19, 2024

Referencing Open CV ticket here: opencv/opencv#13255

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants