MEX memory leak - is this a bug?

조회 수: 1 (최근 30일)
Ayal
Ayal 2013년 9월 2일
댓글: Mateusz Pieniak 2017년 5월 3일
I tried reporting this as a bug, with no response so far. Before I try a little harder, I want to make sure this is indeed a bug. I asked a few people, here & other places, and no one can explain this:
mxGPUArray * tmp=mxGPUCopyFromMxArray(prhs[0]);
plhs[0]=mxGPUCreateMxArrayOnGPU(tmp);
mxGPUDestroyGPUArray(out);
This code causes memory leaks (at least in Matlab 2013a, compiled with Visual Studio 2010). I run the following on loop in matlab:
foo=gpuArray.randn(1000);
res=MyMex(foo);
gpuDevice
And I see that with every iteration, there is less free memory in the GPU device. Correct me if i'm wrong, but this SHOULD NOT be happening. Matlab should free the values previously stored by "res", but It doesn't seem to.
NOTE: This is not bug 954239.
I've been working on this problem for two weeks, with no luck. There's always some memory leak. Does anyone have any idea what's going on? Did I miss something?

채택된 답변

Edric Ellis
Edric Ellis 2013년 9월 4일
It appears as though there's a problem with mxGPUCopyFromMxArray. Please could you try the following workaround:
/**
* Replacement for mxGPUCopyFromMxArray
*/
mxGPUArray * mxGPUCopyFromMxArray2(mxArray const * mx) {
if (mxIsGPUArray(mx)) {
mxGPUArray const * tmp = mxGPUCreateFromMxArray(mx);
mxGPUArray * returnValue = mxGPUCopyGPUArray(tmp);
mxGPUDestroyGPUArray(tmp);
return returnValue;
} else {
return const_cast<mxGPUArray *>(mxGPUCreateFromMxArray(mx));
}
}
  댓글 수: 4
Ayal
Ayal 2013년 9월 8일
This mostly works! I still see some odd behavior as far as the memory goes, but I guess it's just something I don't understand related to the memory managment.
using "clear dumdum" actually releases memory, and after the 2nd time I run the code, memory doesn't disappear anymore. However, using "clear foo2" doesn't release any memory. But all the memory returns after clearing all gpuArrays, but it seems like the major problems are resolved.
I also tried
foo=foo2;
testMyMex;
clear foo;
This won't release memory either. I guess there is more to be fixed. But your solution should be a big help to me. Thank you!
Edric Ellis
Edric Ellis 2013년 9월 9일
It's possible that the remaining memory usage behaviour might simply be artefacts of the allocation pooling.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Edric Ellis
Edric Ellis 2013년 9월 3일
I think this is nothing to do with the MEX interface, and instead to do with the way MATLAB tries to recycle memory allocations on the device, as demonstrated by the following example:
% First, check the free memory on the device
>> dev = gpuDevice(); dev.FreeMemory
ans =
5.5265e+09
% Next, make 100 allocations of 8MB
>> for idx = 1:100, c{idx} = gpuArray.ones(1000); end
>> dev.FreeMemory
ans =
4.7138e+09
% Now, clear all but one of those and check the free memory
>> c(2:end) = [];
>> dev.FreeMemory
ans =
4.7138e+09
% Clear all arrays, and check free memory again
>> clear c
>> dev.FreeMemory
ans =
5.5265e+09
You can see here that freeing some arrays doesn't change the amount of free memory on the device, but when all arrays have been freed then all the memory is returned.
  댓글 수: 6
Ayal
Ayal 2013년 9월 3일
And thank you for your help. (Just to be sure: do you get the same problem, or am I just unlucky and need to update every software related to this in hopes that the problem is some corrupted file?)
Mateusz Pieniak
Mateusz Pieniak 2017년 5월 3일
I have the same issue. Did anybody resolved it? It was 3rd September 2013 and a few years passed...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Conway's Game of Life에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by