Memory leaks in simple MEX function

1 次查看(过去 30 天)
Ayal
Ayal 2013-8-15
I wrote a mex function and noticed that every time I run it, more and more memory disappears from my GPU, even though I overwrite the previous results every time. In my attempts find the source of the problem, I wrote the following code (file is called MexMemoryTrack):
#include "mex.h"
#include "gpu/mxGPUArray.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, mxArray const *prhs[])
{
mxInitGPU();
const mxGPUArray * inp=mxGPUCreateFromMxArray(prhs[0]);
const mxGPUArray * ms=mxGPUCreateFromMxArray(prhs[1]);
const double * inpPtr=(const double*) mxGPUGetDataReadOnly(inp);
const double * masksPtr=(const double*) mxGPUGetDataReadOnly(ms);
mxGPUArray * out=mxGPUCopyFromMxArray(prhs[2]);
double * outPtr=(double* ) mxGPUGetData(out);
plhs[0] = mxGPUCreateMxArrayOnGPU(out);
mxGPUDestroyGPUArray(inp);
mxGPUDestroyGPUArray(ms);
mxGPUDestroyGPUArray(out);
}
I run it using:
foo=gpuArray.zeros([3 3 10000 18]);
foo2=gpuArray.randn([7 7 10000 20]);
foo3=gpuArray.randn([5 5 18 20]);
dumdum=MexMemoryTrack(foo2,foo3,foo);
if I put this code in a loop, all my memory ends up disappearing and I get an "out of memory" error. I can't figure out why. This is a very simple code. I either overwrite data or destroy it using mxGPUDestroyGPUArray.
why would I keep losing memory? clearing dumdum after each execution doesn't help I am using Matlab 2013a and Visual Studio 2010 for compiling.

回答(1 个)

Jill Reese
Jill Reese 2013-8-16
编辑:Jill Reese 2013-8-16
There is a known memory leak in R2013a only. If possible, I would apply the patch associated with external bug report 954239 to see if it helps in your situation.
Best,
Jill
  1 个评论
Ayal
Ayal 2013-8-27
Sadly, this did not solve the problem. I deleted my own answer because it was way off. So far it seems to me like Matlab just doesn't clear the GPU memory when it's overwritten. This is a problem similar to one I've read about - when calling Matlab functions from within a MEX, memory isn't overwritten, but re-allocated.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by