1

why I use mutex protect std::queue in two thread, still memory leak Push is faster than pop, but push after 2000 times, push is stop, pop is always doing, when queue is empty, the memory of this program is 1.3GiB, memory leak enter image description here

class Test {
public:
    std::thread read_thread;
    std::thread write_thread;

    mutable std::mutex mut;
    std::queue<std::shared_ptr<std::vector<float>>> queue;
    void Init() {
        read_thread = std::thread(&Test::PushFunc, this);
        write_thread = std::thread(&Test::PopFunc, this);
    }

    void PushFunc()
    {
        int index = 0;
        while (true) {
            usleep(1000 * 1);
            std::lock_guard<std::mutex> a(mut);
            std::vector<float> sig;
            for (int i = 0; i < 752; ++i) {
                for (int j = 0; j < 480; ++j) {
                    sig.emplace_back(1.0f);
                }
            }

            queue.push(std::make_shared<std::vector<float>>(sig));
            std::cout << "push one\n";
            if (index++ > 2000) {
                break;
            }
        }
    }

    void PopFunc()
    {
        while (true) {
            usleep(1000 * 25);
            std::lock_guard<std::mutex> lk(mut);
            if (!queue.empty()) {
                queue.pop();
                std::cout << "pop one\n";

            } else {
                std::cout << "cannot pop\n";
            }

        }
    }


};


int main ()
{
    Test t;
    t.Init();
    while (true);
}
Arm Strong
  • 13
  • 2
  • your code pushes ~25 times more often than pops. push: usleep(1000 * 1); pop: usleep(1000 * 25); – Alexander Dec 13 '21 at 10:54
  • 1
    You have high memory usage but why do you think you have a memory leak? They're not the same thing. – G.M. Dec 13 '21 at 11:11
  • 1
    I see no leaking in the code. The program just keeps the allocated memory, in case you need it again later. Not immediately returning it back to the OS is an optimization. – BoP Dec 13 '21 at 11:11
  • Relevant: [Will malloc implementations return free-ed memory back to the system?](https://stackoverflow.com/q/2215259/580083). Many similar questions have been asked as well, just use you search engine to find them. – Daniel Langr Dec 13 '21 at 11:29

1 Answers1

0

thx to Daniel Langr, I used

malloc_trim(0);

solve this memory problem.

class Test {
public:
    std::thread read_thread;
    std::thread write_thread;
    std::thread clear;


    mutable std::mutex mut;
    std::list<std::shared_ptr<std::vector<float>>> queue;
    void Init() {
        read_thread = std::thread(&Test::PushFunc, this);
        write_thread = std::thread(&Test::PopFunc, this);
        clear =  std::thread(&Test::ClearFunc, this);
    }

    void PushFunc()
    {
        int index = 0;
        while (true) {
            usleep(1000 * 1);
            auto p = std::make_shared<std::vector<float>>();
            for (int i = 0; i < 752; ++i) {
                for (int j = 0; j < 480; ++j) {
                    p->emplace_back(1.0f);
                }
            }
            std::unique_lock<std::mutex> a(mut);
            queue.emplace_back(p);
            std::cout << "push one: " << queue.size() << "\n";
            if (index++ > 2000) {
                break;
            }
        }
    }

    void PopFunc()
    {
        while (true) {
            usleep(1000 * 25);
            std::unique_lock<std::mutex> lk(mut);
            if (!queue.empty()) {
                queue.pop_front();
                std::cout << "pop one: " << queue.size() << "\n";
            } else {
                std::cout << "cannot pop\n";
            }
        }
    }

    void ClearFunc()
    {
        while (true) {
            sleep(1);
            malloc_trim(0);
        }
    }
};

Arm Strong
  • 13
  • 2