释放CUDA内存Matlab [英] Releasing CUDA memory Matlab

查看:947
本文介绍了释放CUDA内存Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在GPU中分配的内存不会释放。
我使用以下命令:

I have noticed that memory allocated in the GPU is not released. I used the following command:

A=gpuArray.randn(1e4);
gpuDevice

FreeMemory:3.3336e + 09

FreeMemory: 3.3336e+09

A=A*2

FreeMemory: 2.5336e + 09

FreeMemory: 2.5336e+09

看起来像MATLAB为A * 2分配内存,但是没有释放A的内存。我看不到如何释放内存不再使用。对于 A = A * A',问题更糟糕,因为它似乎MATLAB分配更多的内存(可能为A'),不释放它。
我在互联网上发现,这是一个已知的错误(或类似的错误?)在MATLAB 2013b解决(见 http://www.mathworks.com/support/bugreports/954239 ),但我使用的是MATLAB 2014a。我也从链接安装了zip文件,但它没有帮助。

Looks like MATLAB allocated memory for A*2, but did not release the memory for A. I don't see how I can release the memory that is no longer in use. The problem is even worse for A=A*A' as it seems that MATLAB allocates even more memory (probably for A') and does not release it. I found on the internet, that this is a known bug (or a similar bug?) that was solved in MATLAB 2013b (see http://www.mathworks.com/support/bugreports/954239), but I am using MATLAB 2014a. I also installed the zip file from the link, but it did not help.

我会感谢任何帮助。

推荐答案

通过运行如下代码:

d = gpuDevice;
A = gpuArray.rand(1e4);
freeMem = NaN(1, 11);
freeMem(1) = d.FreeMemory;
for idx = 2:11
    A = A * 2;
    wait(d);
    freeMem(idx) = d.FreeMemory;
end
plot(1:11, freeMem / 1e9, 'b-', ...
     [1 11], [d.TotalMemory, d.TotalMemory]/1e9, 'r-');
legend({'Free Memory', 'Total Memory'});
xlabel('Iteration');
ylabel('Memory (GB)');

你可以看到,MATLAB不是立即释放内存, ever:

You can see that MATLAB is not immediately releasing the memory, but neither is it holding on to it for ever:

如果你使 A 更大,你也会发现不同的行为。我的GPU有〜6GB的RAM,如果我使 A = gpuArray.rand(16000),那么我看到内存似乎立即释放。

You will also find different behaviour if you make A larger. My GPU has ~6GB of RAM, and if I make A = gpuArray.rand(16000), then I see that the memory appears to be released immediately.

顺便提一句,你提到的错误报告实际上是指主机内存不可恢复地泄露,而不是设备内存。

By the way, the bug report you refer to is actually talking about host memory being irretrievably leaked, not device memory.

您的算法中实际用完了设备内存吗?

Are you actually running out of device memory in your algorithm?

这篇关于释放CUDA内存Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆