How to solve the issue of high memory usage when writing too many frames using FFmpeg as the API Backend for OpenCV 4.7 VideoWriter on Linux[CANCELED]

I encountered a issue of high memory usage when writing too many frames using FFmpeg as the API Backend for OpenCV 4.7 VideoWriter on Linux, The pseudo code is as follows:

    cv::VideoWriter writer(video_path, video_fourcc, video_fps, video_size);
    int32_t frame_nums = 10000;
    for (int32_t i = 0; i < frame_nums; ++i) {
        cv::Mat frame;

        // some operations
        // ...

        writer.write(frame);
        frame.release();
    }
    writer.release();

As the number of frames written increases, the memory usage becomes larger and larger, and I can not reduce the memory usage.

I try to release the frame after writing it but it does not work, and I do not find anything like flush or any way to set the buffer size of VideoWriter.

How to solve it?

that pseudo code contains no issues.

you’ll need to present actual code, a MRE, that is debuggable. also all relevant information you have about the environment.

and especially quantify “high memory usage”/“larger and larger” in space and time.

Thanks for your reply. I was misled before, and I find the real reason of high memory now, it’s not opencv problem.