You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Uncontrolled spawning of threads that uses OpenCV should be avoided. This is a bad programming practice.
OpenCV widely uses TLS data.
Initialization of thread is quite expensive operation, so consider to use thread pools instead for better experience.
BTW, This code with 1M threads consumes 400Mb in peak:
for (size_t i = 0; i < 1000000; i++)
{
std::async(std::launch::async, []() {
std::cout << cv::utils::getThreadID() << std::endl; // init TLS structures
cv::Mat::zeros(1, 1, CV_32FC3);
}).wait();
}
Thank you for your reply @alalek, I thought this was the case. :-)
It is still unfortunate that the TLS data are not freed on thread termination, because in long running multithreaded applications one constantly looses memory with every call to std::async (which I consider to be a pretty standard practice if I am not doing something like parallel_for). The provided example was quite extreme, of course, but in my original code, I have noticed the leak after about 12 hours of intensive computation. If there is a list of feature requests, please consider this one and feel free to close this issue.
@alalek
Apologies to add on to a closed thread, but a simple question: Is there a way to compile opencv so that it does not use TLS? I took a quick look at the source and noticed mostly it is used in the mutexes sections and google's protobuf.
Of course spawning threads uncontrollably is bad, but many times on a supercomputer or cluster, one will want to try many different instances of a model that includes some opencv code, and restarting the entire program (including loading all shared library calls etc.) N times is much more expensive than having a simple wrapper that spawns a new thread for each model one wants to try.
Each model itself is "smart" in that it will only spawn a thread pool for itself, but if you do that 50 billion times (as is often the case for a large sweep of parameters of optimization) the memory leak gets quite large.
In our haskell-opencv binding we're also suffering from this memory leak. This leak is especially troublesome in Haskell where the runtime system by default executes FFI calls (calls from Haskell to C++) on random available OS threads. The user has no control over the lifetime of these OS threads which means there's a high risk of leaking OpenCV TLS data.
I was surprised by this too (especially since heap checkers don't usually track statics). std::async is arguably the wrong tool sometimes, but it's so convenient to grab compared to thread pools, which are unfortunately not in std C++.
Activity
alalek commentedon Sep 29, 2017
Uncontrolled spawning of threads that uses OpenCV should be avoided. This is a bad programming practice.
OpenCV widely uses TLS data.
Initialization of thread is quite expensive operation, so consider to use thread pools instead for better experience.
BTW, This code with 1M threads consumes 400Mb in peak:
FloopCZ commentedon Sep 29, 2017
Thank you for your reply @alalek, I thought this was the case. :-)
It is still unfortunate that the TLS data are not freed on thread termination, because in long running multithreaded applications one constantly looses memory with every call to
std::async(which I consider to be a pretty standard practice if I am not doing something likeparallel_for). The provided example was quite extreme, of course, but in my original code, I have noticed the leak after about 12 hours of intensive computation. If there is a list of feature requests, please consider this one and feel free to close this issue.use only one thread to process inages in object classification
flyingfalling commentedon Jan 4, 2019
@alalek
Apologies to add on to a closed thread, but a simple question: Is there a way to compile opencv so that it does not use TLS? I took a quick look at the source and noticed mostly it is used in the mutexes sections and google's protobuf.
Of course spawning threads uncontrollably is bad, but many times on a supercomputer or cluster, one will want to try many different instances of a model that includes some opencv code, and restarting the entire program (including loading all shared library calls etc.) N times is much more expensive than having a simple wrapper that spawns a new thread for each model one wants to try.
Each model itself is "smart" in that it will only spawn a thread pool for itself, but if you do that 50 billion times (as is often the case for a large sweep of parameters of optimization) the memory leak gets quite large.
basvandijk commentedon Jan 5, 2019
I would like to know this as well.
In our haskell-opencv binding we're also suffering from this memory leak. This leak is especially troublesome in Haskell where the runtime system by default executes FFI calls (calls from Haskell to C++) on random available OS threads. The user has no control over the lifetime of these OS threads which means there's a high risk of leaking OpenCV TLS data.
tdp2110 commentedon Aug 13, 2019
I was surprised by this too (especially since heap checkers don't usually track statics).
std::asyncis arguably the wrong tool sometimes, but it's so convenient to grab compared to thread pools, which are unfortunately not in std C++.xingxing-2019 commentedon Dec 16, 2019
thank you so much!
17e2bf5 when would this patch can merge to 4.1.0