mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

MXNet C++Interface reasoning leads to CPU memory leak

Open chenyujing1234 opened this issue 8 years ago • 39 comments

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:

  1. If only do MXPredCreate, then MXPredFree, and MXPredCreate gives different width and height. After 100 thousand cycles, memory will increase by 1G
  2. 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.

chenyujing1234 avatar Nov 14 '18 16:11 chenyujing1234

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

zachgk avatar Nov 14 '18 19:11 zachgk

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

KellenSunderland avatar Nov 14 '18 20:11 KellenSunderland

@mxnet-label-bot update [Bug, C API, Memory, Pending Requester Info]

leleamol avatar Nov 15 '18 01:11 leleamol

You can get my test code from this address. TKS https://pan.baidu.com/s/19Wtd_Cf1BGF-2MS3le1mYA

chenyujing1234 avatar Nov 15 '18 11:11 chenyujing1234

@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 avatar Nov 15 '18 11:11 chenyujing1234

@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

chenyujing1234 avatar Nov 15 '18 11:11 chenyujing1234

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

You can get my test code from this address. TKS https://pan.baidu.com/s/19Wtd_Cf1BGF-2MS3le1mYA

chenyujing1234 avatar Nov 15 '18 11:11 chenyujing1234

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.

KellenSunderland avatar Nov 15 '18 18:11 KellenSunderland

Are you using a mxnet version that contain MKL-DNN?

chinakook avatar Nov 16 '18 06:11 chinakook

Are you using a mxnet version that contain MKL-DNN?

No

chenyujing1234 avatar Nov 16 '18 07:11 chenyujing1234

Great

Now my project is stuck here, waiting for your reply. Thank you

chenyujing1234 avatar Nov 19 '18 02:11 chenyujing1234

Any chance you could paste the reproducing code on gist.github.com?

KellenSunderland avatar Nov 19 '18 02:11 KellenSunderland

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

chenyujing1234 avatar Nov 19 '18 02:11 chenyujing1234

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 avatar Nov 19 '18 02:11 KellenSunderland

@KellenSunderland I uploaded the test code attached by @chenyujing1234 to google drive , you can get it by this link

AvenSun avatar Nov 19 '18 04:11 AvenSun

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

chenyujing1234 avatar Nov 19 '18 13:11 chenyujing1234

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

KellenSunderland avatar Nov 19 '18 19:11 KellenSunderland

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';

chenyujing1234 avatar Nov 20 '18 02:11 chenyujing1234

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 ?

chenyujing1234 avatar Nov 21 '18 01:11 chenyujing1234

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?

chenyujing1234 avatar Nov 22 '18 06:11 chenyujing1234