MXNet C++Interface reasoning leads to CPU memory leak
I am using the c++ interface of mxnet to carry out the reasoning of mtcnn algorithm for face detection. Environment: Ubuntu16.04.1 + GPU cuda9.0 + MXNet1.3.0 I use the interface: MXPredCreate MXPredReshape MXPredForward MXPredFree... When I used a lot of pictures for stress testing (running for a long time), I found that my process occupied more and more CPU memory, and eventually it occupied all the memory. My process was forced to kill by the system. Later, I wrote the program To Test:
- If only do MXPredCreate, then MXPredFree, and MXPredCreate gives different width and height. After 100 thousand cycles, memory will increase by 1G
- If you do MXPredCreate, and then MXPredReshape to change the width and height, so that the continuous cycle, found that memory leaks very quickly, less than half an hour to leak 4G. Do you want to invite MXNet's main developers to find this problem? Please track it down for me. It has been bothering me for almost a week.
@mxnet-label-bot add [C++, Memory, Bug, Pending Requester Info]
Thanks for raising the issue @chenyujing1234. Can you provide a bit more info so we can better help?
Environment info (Required)
What to do:
1. Download the diagnosis script from https://raw.githubusercontent.com/apache/incubator-mxnet/master/tools/diagnose.py
2. Run the script using `python diagnose.py` and paste its output here.
Build info (If built from source)
Compiler (gcc/clang/mingw/visual studio):
MXNet commit hash:
(Paste the output of git rev-parse HEAD here.)
Build config: (Paste the content of config.mk, or the build command.)
Minimum reproducible example
Can you share the code for the program that you used to test the memory leak?
@chenyujing1234 Would you be able to post some sample code, that when run in a loop will leak? If we can run this for a fixed number of iterations (say 1234 loops) we should then be able to look with an ASAN build and see what's leaking.
@mxnet-label-bot update [Bug, C API, Memory, Pending Requester Info]
You can get my test code from this address. TKS https://pan.baidu.com/s/19Wtd_Cf1BGF-2MS3le1mYA
@mxnet-label-bot update [Bug, C API, Memory, Pending Requester Info] You can get my test code from this address. TKS https://pan.baidu.com/s/19Wtd_Cf1BGF-2MS3le1mYA
@chenyujing1234 Would you be able to post some sample code, that when run in a loop will leak? If we can run this for a fixed number of iterations (say 1234 loops) we should then be able to look with an ASAN build and see what's leaking.
You can get my test code from this address. TKS https://pan.baidu.com/s/19Wtd_Cf1BGF-2MS3le1mYA
@mxnet-label-bot add [C++, Memory, Bug, Pending Requester Info]
Thanks for raising the issue @chenyujing1234. Can you provide a bit more info so we can better help?
Environment info (Required)
What to do: 1. Download the diagnosis script from https://raw.githubusercontent.com/apache/incubator-mxnet/master/tools/diagnose.py 2. Run the script using `python diagnose.py` and paste its output here.Build info (If built from source)
Compiler (gcc/clang/mingw/visual studio):
MXNet commit hash: (Paste the output of
git rev-parse HEADhere.)Build config: (Paste the content of config.mk, or the build command.)
Minimum reproducible example
Can you share the code for the program that you used to test the memory leak?
You can get my test code from this address. TKS https://pan.baidu.com/s/19Wtd_Cf1BGF-2MS3le1mYA
Great bug report @chenyujing1234. Really appreciate the details. I'll take a look at this on a best-effort basis (might take a while to verify the leaks on my end).
If anyone else from the community has time to jump on this I think it'd be very useful. I've seen a lot of reports of leaks from users.
Are you using a mxnet version that contain MKL-DNN?
Are you using a mxnet version that contain MKL-DNN?
No
Great
Now my project is stuck here, waiting for your reply. Thank you
Any chance you could paste the reproducing code on gist.github.com?
Any chance you could paste the reproducing code on gist.github.com?
I am sorry , I can't visit gist.github.com. Can you visit https://pan.baidu.com/s/19Wtd_Cf1BGF-2MS3le1mYA
Looks like I need to install an app to access the files, but no worries, I can likely download the code with a colleague tomorrow.
@KellenSunderland I uploaded the test code attached by @chenyujing1234 to google drive , you can get it by this link
Looks like I need to install an app to access the files, but no worries, I can likely download the code with a colleague tomorrow.
I Build ASAN versin, and test, Found memory leak. the log is : https://gist.github.com/chenyujing1234/0449ecf6f502e5c3538e4f2f018a04e1
Great work, looks like the ASAN build is setup right and detecting errors. What I would do now is run the offending code in a loop with a fixed number of iterations, then look for a leak that has a multiple of that number. If there's a genuine leak it should be present there. For example you could run MXPredCreate, MXPredReshape, MXPredForward and then MXPredFree in a loop 123 times, then look for mem leaks with a multiple of 123 instances.
This guide may also help you: https://cwiki.apache.org/confluence/display/MXNET/Detecting+Memory+Leaks+and+Buffer+Overflows+in+MXNet
Great work, looks like the ASAN build is setup right and detecting errors. What I would do now is run the offending code in a loop with a fixed number of iterations, then look for a leak that has a multiple of that number. If there's a genuine leak it should be present there. For example you could run MXPredCreate, MXPredReshape, MXPredForward and then MXPredFree in a loop 123 times, then look for mem leaks with a multiple of 123 instances.
This guide may also help you: https://cwiki.apache.org/confluence/display/MXNET/Detecting+Memory+Leaks+and+Buffer+Overflows+in+MXNet Some of my test code need to be modified, otherwise the memory overflow error will be reported. include/mxnet_mtcnn.hpp 42 buffer_.reset(new char[length_ + 1]); 43 ifs.read(buffer_.get(), length_); 44 ifs.close(); 45 buffer_[length_] = '\0';
Great work, looks like the ASAN build is setup right and detecting errors. What I would do now is run the offending code in a loop with a fixed number of iterations, then look for a leak that has a multiple of that number. If there's a genuine leak it should be present there. For example you could run MXPredCreate, MXPredReshape, MXPredForward and then MXPredFree in a loop 123 times, then look for mem leaks with a multiple of 123 instances.
This guide may also help you: https://cwiki.apache.org/confluence/display/MXNET/Detecting+Memory+Leaks+and+Buffer+Overflows+in+MXNet
Has any result ?
Great work, looks like the ASAN build is setup right and detecting errors. What I would do now is run the offending code in a loop with a fixed number of iterations, then look for a leak that has a multiple of that number. If there's a genuine leak it should be present there. For example you could run MXPredCreate, MXPredReshape, MXPredForward and then MXPredFree in a loop 123 times, then look for mem leaks with a multiple of 123 instances.
This guide may also help you: https://cwiki.apache.org/confluence/display/MXNET/Detecting+Memory+Leaks+and+Buffer+Overflows+in+MXNet
I've solved one the memory leak problem caused by using reshape functions in the CPU version. Cause: PredictorHandle object is not released after calling Reshape.
But there will still be memory leaks in reasoning on gpu, but I don't know how to compile the version of ASAN. Can you help me to compile the version of ASAN of GPU-cuda 9.0?