Skip to content

Memory leak in every thread #9745

Closed
Closed

Description

@FloopCZ
  • OpenCV => 3.3.0
  • Operating System / Platform => Arch Linux (4.12.13-1-ARCH SMP PREEMPT x86_64 GNU/Linux)
  • Compiler => gcc 7.2.0 and clang 5.0.0
Detailed description

It seems that a significant portion of memory is leaked in every thread.
The following code snippet consumes cca 100MB/s.

Steps to reproduce
#include <opencv2/opencv.hpp>
#include <future>
int main()
{
    while(true) {
        std::async(std::launch::async, []() { cv::Mat::zeros(1, 1, CV_32FC3); }).wait();
    }
}

Activity

  1. alalek commented on Sep 29, 2017

    @alalek
    Member

    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();
        }
    
  2. FloopCZ commented on Sep 29, 2017

    @FloopCZ
    Author

    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.

  3. flyingfalling commented on Jan 4, 2019

    @flyingfalling
    Contributor

    @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.

  4. basvandijk commented on Jan 5, 2019

    @basvandijk

    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.

  5. tdp2110 commented on Aug 13, 2019

    @tdp2110
    Contributor

    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++.

  6. xingxing-2019 commented on Dec 16, 2019

    @xingxing-2019

    thank you so much!
    17e2bf5 when would this patch can merge to 4.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions