MEX memory leak - is this a bug?
Mostrar comentarios más antiguos
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?
Respuesta aceptada
Más respuestas (1)
Edric Ellis
el 3 de Sept. de 2013
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 comentarios
Edric Ellis
el 3 de Sept. de 2013
I was using R2013a with (as you observe) a pretty large GPU. The memory pooling is parameterized on the total amount of memory on the GPU, so you can expect some differences in detail.
The intention of the memory pooling is that you should never run out of memory because of the allocations that MATLAB no longer needs, but haven't actually been freed from the device.
If your code eventually fails because it runs out of memory, then there's a real problem - if so, please could you post a minimal complete self-contained example. Obviously, by using const_cast, you are living dangerously. The CUDAKernel has safe support for in-place operations (you can have in-out parameters to the FEVAL method) whereas the MEX interface does not.
Edric Ellis
el 3 de Sept. de 2013
Thanks for that - I'll take a look and get back to you.
Ayal
el 3 de Sept. de 2013
Mateusz Pieniak
el 3 de Mayo de 2017
I have the same issue. Did anybody resolved it? It was 3rd September 2013 and a few years passed...
Categorías
Más información sobre GPU CUDA and MEX Programming en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!