So my project use OpenCvSharp (wrapper for OpenCv). My project heavily focusing on image processing where all the images are query from a VideoCapture object. But i notice that my project has memory leak where the memory usage climb up 1~2 MB per second which is a big no no. I can force garbage collection but it’s really expensive (from 20 milliseconds to 100+ milliseconds per frame). I tried dot memory to track down the problem but it only track managed memory. So is there any way to track unmanaged memory in C# ? And do u have any tips to avoid this problem ?
For anyone asking for the code. It’s hundreds line long, so raping everyone eyes with shit tons of code is a no no. I tried to narrow down the scope to look for the problem but i can’t really found the piece of code that’s causing the problem. Thank you for all of your answers
And do u have any tips to avoid this problem ?
OpenCvSharp probably has a bunch of objects that implement IDisposable
— make sure to dispose those (typically, with using
blocks).
Personally, I’ve found https://memprofiler.com to be quite good, especially for a WinForms app (as it shows warnings specific to common gotchas). Maybe it’ll help you out for this, too.
Thanks for your answer. Gonna check it out soon
I’ve used ANTS memory profiler in the past and it saved my ass many times. Did a quick check and it looks like it should do what you need.
https://documentation.red-gate.com/amp8/strategies-for-memory-profiling/checking-unmanaged-memory-usage
Thank you, i’m gonna check it out now
ANTS memory profiler
Wow. Not at that price.
I don’t mind paying a few bucks, but it would be used for personal stuff, not used in a multi-national org with pockets deep enough that those kinds of prices would be rounding errors.
windbg
To avoid memory leaks, you need to pay attention to de-allocate unmanaged memory when you stopped using them – see IDisposable
edit: https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/unmanaged
windbg
Thanks, i’m gonna check it out now
To avoid memory leaks, you need to pay attention to de-allocate unmanaged memory when you stopped using them – see IDisposable
Yes i do dispose unnecessary stuff but somehow the memory leak still happening
Can’t help with the general question but I had the same issue while using the java wrapper for openCV. The solution was to explicitly call release on my Mat
objects. In your case in C#, try using using
every time you create a Mat object if you are not already, it might be enough to fix your issue
Hmm, i’m gonna try it
Last time I used Windows Performance Toolkit to track down a memory leak.
This article descripes how to get all the stack traces where any application is allocating memory.
Note that you have to download and install the Windows ADK first.
Thank you, i’m trying with the ANTS profiler a bit and gonna try the WPT soon
Upvoted for visibility. I, too, have a sneaky suspicion that an effective answer will be useful in my own future work.
Still,
It’s hundreds line long, so raping everyone eyes with shit tons of code is a no no.
Wow. LMAO.
Upvoted for visibility. I, too, have a sneaky suspicion that an effective answer will be useful in my own future work.
u/razordreamz mentioned ANTS memory profiler so u can check that out
Wow. LMAO.
🙂