Excessive memory leak even after calling GC.Collect()

I am using Opencvsharp library and running a WPF Desktop application where i load image with 20/25 faces . Run face detection on this image and after that execute emotion analysis on these detected faces based on onnx ml model . this functions execute every 5 seconds . every 10 seconds to release unmanaged resources i run.

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

the issue is the program run fine for 1.5/2 hours . after that memory increases gradually . memory is also released periodically . but the memory increases at a much more rate than it releases . After 3 or 4 hours the memory reaches to almost 10 gb and applications crashes with failed memory allocation exception message . one thing i have noticed with single face image memory pressure is better than 25 faces images . it also increased by almost 1 gb but it never exceeds absurd amount of memory . has anyone faced similar kind of problem ?

Environment

windows 10 and windows 11 version 64 bit
Visual Studio 2019 . net framework 4.7.2 . WPF desktop application

Hi,
not knowing neither .net, nor Opencvsharp, my answer might be of limited help, but there are still a few generic things you might do (in the worst case, it will reduce the possibilities when some else comes to help you) :

  1. I would check if the memory is really free for deletion (maybe you are still referencing it somewhere)
  2. check if garbage collector can properly delete these objects (do a simple infinite loop : just create an object, delete it as you do in your program, and run GC ; see if memory increases or not). If not, maybe you need to call some ā€œrelease ressourcesā€ function
  3. If you have a memory profile (in C++ on linux there is valgrind memcheck, no idea for equivalents for .net on windows), then try to see where memory gets alocated
  4. try to get a minimal code to reproduce the issue : often doing this you find you problem, and if not, post it on the forum, and it will be far easier for others to spot you problem than without any code or with a huge codebase
1 Like