Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to release the memory in cv::Mat

There are some memory leak in my project, after several round of analysis, it seems the memory in cv::Mat not be released.

My test code like this:

Mat img = imread("./a.jpg");
img.release();  //optional, both tested and got the similar result.

After running I got the mem leak msg from valgrind whether I called img.release explicitly or not: ==27271== Memcheck, a memory error detector ==27271== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. ==27271== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info ==27271== Command: ./a.out ==27271== Parent PID: 27189 ==27271== ==27271== ==27271== HEAP SUMMARY: ==27271== in use at exit: 72 bytes in 3 blocks ==27271== total heap usage: 272 allocs, 269 frees, 1,467,134 bytes allocated ==27271== ==27271== 8 bytes in 1 blocks are still reachable in loss record 1 of 3 ==27271== at 0x4845394: operator new(unsigned long) (vg_replace_malloc.c:334) ==27271== by 0x4A3B78F: cv::getInitializationMutex() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x489E077: _GLOBAL__sub_I_system.cpp (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x400DB0F: call_init.part.0 (dl-init.c:72) ==27271== by 0x400DC5F: call_init (dl-init.c:30) ==27271== by 0x400DC5F: _dl_init (dl-init.c:120) ==27271== by 0x4000D43: ??? (in /lib/aarch64-linux-gnu/ld-2.24.so) ==27271== ==27271== 8 bytes in 1 blocks are still reachable in loss record 2 of 3 ==27271== at 0x4845394: operator new(unsigned long) (vg_replace_malloc.c:334) ==27271== by 0x499DC3F: cv::Mat::getStdAllocator() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x499DC8B: cv::Mat::getDefaultAllocator() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x499E62B: cv::Mat::create(int, int const, int) (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x4B1353F: cv::imread_(cv::String const&, int, int, cv::Mat) (in /usr/local/lib/libopencv_imgcodecs.so.3.1.0) ==27271== by 0x4B14457: cv::imread(cv::String const&, int) (in /usr/local/lib/libopencv_imgcodecs.so.3.1.0) ==27271== by 0x10907F: main (main.cc:14) ==27271== ==27271== 56 bytes in 1 blocks are still reachable in loss record 3 of 3 ==27271== at 0x4845394: operator new(unsigned long) (vg_replace_malloc.c:334) ==27271== by 0x4A3B717: cv::Mutex::Mutex() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x4A3B797: cv::getInitializationMutex() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x489E077: _GLOBAL__sub_I_system.cpp (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x400DB0F: call_init.part.0 (dl-init.c:72) ==27271== by 0x400DC5F: call_init (dl-init.c:30) ==27271== by 0x400DC5F: _dl_init (dl-init.c:120) ==27271== by 0x4000D43: ??? (in /lib/aarch64-linux-gnu/ld-2.24.so) ==27271== ==27271== LEAK SUMMARY: ==27271== definitely lost: 0 bytes in 0 blocks ==27271== indirectly lost: 0 bytes in 0 blocks ==27271== possibly lost: 0 bytes in 0 blocks ==27271== still reachable: 72 bytes in 3 blocks ==27271== suppressed: 0 bytes in 0 blocks ==27271== ==27271== For counts of detected and suppressed errors, rerun with: -v ==27271== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Then I changed my code and monitor the memory state every 5 seconds on my board like this:

while(1){
   Mat img = imread("./a.jpg");
   img.release(); //optional, both tested and get the similar result  
}

The total memory leak about 2M in 30 minutes, please find the result in attached file imread.png.

My test image is 640x480 with 71KB size. I know there will be a refcount decrease in Mat::release() to fastfree Mat::u->data(Mat::data), but it seems where still some fragmental mem leaks, or do I miss something.

Greately appreciate your help. Thanks.

C:\fakepath\imread.png

How to release the memory in cv::Mat

There are some memory leak in my project, after several round of analysis, it seems the memory in cv::Mat not be released.

My test code like this:

Mat img = imread("./a.jpg");
img.release();  //optional, both tested and got the similar result.

After running I got the mem leak msg from valgrind whether I called img.release explicitly or not: not:

==27271== Memcheck, a memory error detector ==27271== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. ==27271== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info ==27271== Command: ./a.out ==27271== Parent PID: 27189 ==27271== ==27271== ==27271== HEAP SUMMARY: ==27271== in use at exit: 72 bytes in 3 blocks ==27271== total heap usage: 272 allocs, 269 frees, 1,467,134 bytes allocated ==27271== ==27271== 8 bytes in 1 blocks are still reachable in loss record 1 of 3 ==27271== at 0x4845394: operator new(unsigned long) (vg_replace_malloc.c:334) ==27271== by 0x4A3B78F: cv::getInitializationMutex() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x489E077: _GLOBAL__sub_I_system.cpp (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x400DB0F: call_init.part.0 (dl-init.c:72) ==27271== by 0x400DC5F: call_init (dl-init.c:30) ==27271== by 0x400DC5F: _dl_init (dl-init.c:120) ==27271== by 0x4000D43: ??? (in /lib/aarch64-linux-gnu/ld-2.24.so) ==27271== ==27271== 8 bytes in 1 blocks are still reachable in loss record 2 of 3 ==27271== at 0x4845394: operator new(unsigned long) (vg_replace_malloc.c:334) ==27271== by 0x499DC3F: cv::Mat::getStdAllocator() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x499DC8B: cv::Mat::getDefaultAllocator() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x499E62B: cv::Mat::create(int, int const, int) (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x4B1353F: cv::imread_(cv::String const&, int, int, cv::Mat) (in /usr/local/lib/libopencv_imgcodecs.so.3.1.0) ==27271== by 0x4B14457: cv::imread(cv::String const&, int) (in /usr/local/lib/libopencv_imgcodecs.so.3.1.0) ==27271== by 0x10907F: main (main.cc:14) ==27271== ==27271== 56 bytes in 1 blocks are still reachable in loss record 3 of 3 ==27271== at 0x4845394: operator new(unsigned long) (vg_replace_malloc.c:334) ==27271== by 0x4A3B717: cv::Mutex::Mutex() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x4A3B797: cv::getInitializationMutex() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x489E077: _GLOBAL__sub_I_system.cpp (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x400DB0F: call_init.part.0 (dl-init.c:72) ==27271== by 0x400DC5F: call_init (dl-init.c:30) ==27271== by 0x400DC5F: _dl_init (dl-init.c:120) ==27271== by 0x4000D43: ??? (in /lib/aarch64-linux-gnu/ld-2.24.so) ==27271== ==27271== LEAK SUMMARY: ==27271== definitely lost: 0 bytes in 0 blocks ==27271== indirectly lost: 0 bytes in 0 blocks ==27271== possibly lost: 0 bytes in 0 blocks ==27271== still reachable: 72 bytes in 3 blocks ==27271== suppressed: 0 bytes in 0 blocks ==27271== ==27271== For counts of detected and suppressed errors, rerun with: -v ==27271== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Then I changed my code and monitor the memory state every 5 seconds on my board like this:

while(1){
   Mat img = imread("./a.jpg");
   img.release(); //optional, both tested and get the similar result  
}

The total memory leak about 2M in 30 minutes, please find the result in attached file imread.png.

My test image is 640x480 with 71KB size. I know there will be a refcount decrease in Mat::release() to fastfree Mat::u->data(Mat::data), but it seems where still some fragmental mem leaks, or do I miss something.

Greately appreciate your help. Thanks.

C:\fakepath\imread.png

click to hide/show revision 3
None

updated 2018-12-06 01:37:16 -0600

berak gravatar image

How to release the memory in cv::Mat

There are some memory leak in my project, after several round of analysis, it seems the memory in cv::Mat not be released.

My test code like this:

Mat img = imread("./a.jpg");
img.release();  //optional, both tested and got the similar result.

After running I got the mem leak msg from valgrind whether I called img.release explicitly or not:

not: ==27271== Memcheck, a memory error detector ==27271== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. ==27271== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info ==27271== Command: ./a.out ==27271== Parent PID: 27189 ==27271== ==27271== ==27271== HEAP SUMMARY: ==27271== in use at exit: 72 bytes in 3 blocks ==27271== total heap usage: 272 allocs, 269 frees, 1,467,134 bytes allocated ==27271== ==27271== 8 bytes in 1 blocks are still reachable in loss record 1 of 3 ==27271== at 0x4845394: operator new(unsigned long) (vg_replace_malloc.c:334) ==27271== by 0x4A3B78F: cv::getInitializationMutex() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x489E077: _GLOBAL__sub_I_system.cpp (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x400DB0F: call_init.part.0 (dl-init.c:72) ==27271== by 0x400DC5F: call_init (dl-init.c:30) ==27271== by 0x400DC5F: _dl_init (dl-init.c:120) ==27271== by 0x4000D43: ??? (in /lib/aarch64-linux-gnu/ld-2.24.so) ==27271== ==27271== 8 bytes in 1 blocks are still reachable in loss record 2 of 3 ==27271== at 0x4845394: operator new(unsigned long) (vg_replace_malloc.c:334) ==27271== by 0x499DC3F: cv::Mat::getStdAllocator() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x499DC8B: cv::Mat::getDefaultAllocator() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x499E62B: cv::Mat::create(int, int const, const*, int) (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x4B1353F: cv::imread_(cv::String const&, int, int, cv::Mat) cv::Mat*) (in /usr/local/lib/libopencv_imgcodecs.so.3.1.0) ==27271== by 0x4B14457: cv::imread(cv::String const&, int) (in /usr/local/lib/libopencv_imgcodecs.so.3.1.0) ==27271== by 0x10907F: main (main.cc:14) ==27271== ==27271== 56 bytes in 1 blocks are still reachable in loss record 3 of 3 ==27271== at 0x4845394: operator new(unsigned long) (vg_replace_malloc.c:334) ==27271== by 0x4A3B717: cv::Mutex::Mutex() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x4A3B797: cv::getInitializationMutex() (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x489E077: _GLOBAL__sub_I_system.cpp (in /usr/local/lib/libopencv_core.so.3.1.0) ==27271== by 0x400DB0F: call_init.part.0 (dl-init.c:72) ==27271== by 0x400DC5F: call_init (dl-init.c:30) ==27271== by 0x400DC5F: _dl_init (dl-init.c:120) ==27271== by 0x4000D43: ??? (in /lib/aarch64-linux-gnu/ld-2.24.so) ==27271== ==27271== LEAK SUMMARY: ==27271== definitely lost: 0 bytes in 0 blocks ==27271== indirectly lost: 0 bytes in 0 blocks ==27271== possibly lost: 0 bytes in 0 blocks ==27271== still reachable: 72 bytes in 3 blocks ==27271== suppressed: 0 bytes in 0 blocks ==27271== ==27271== For counts of detected and suppressed errors, rerun with: -v ==27271== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

0)

Then I changed my code and monitor the memory state every 5 seconds on my board like this:

while(1){
   Mat img = imread("./a.jpg");
   img.release(); //optional, both tested and get the similar result  
}

The total memory leak about 2M in 30 minutes, please find the result in attached file imread.png.

My test image is 640x480 with 71KB size. I know there will be a refcount decrease in Mat::release() to fastfree Mat::u->data(Mat::data), but it seems where still some fragmental mem leaks, or do I miss something.

Greately appreciate your help. Thanks.

C:\fakepath\imread.png