Matlab Out of memory问题总结【原创】(二)

  [复制链接]
wangjuan313 发表于 2008-12-3 22:27:04
不知道为什么论坛里没有关于MATLAB常见问题的一些总结贴,我在这里抛砖引玉了,希望各位高手能够不吝指教,多多发表自己对MATLAB使用的一些感悟和总结!


Out of memory问题总结【原创】(二)
1.
尽量早的分配大的矩阵变量
Matlab使用heap method管理内存。当在Matlab heap中没有足够的内存使用时,它会向系统请求内存。但是只要内存碎片可以存下当前的变量,Matlab会重新使用内存。
比如:
a = rand(1e6,1);
b = rand(1e6,1);
使用大约15.4 MB RAM

c = rand(2.1e6,1);使用近似16.4 MB RAM:

a = rand(1e6,1);
b = rand(1e6,1);
clear
c = rand(2.1e6,1);
使用32.4 MB RAM。因为Matlab不能使用abclear的空间,因为它们均小于2.1 MB,而同时它们也很可能是不连续的。
最好的方法:
c = rand(2.1e6,1);
clear
a = rand(1e6,1);
b = rand(1e6,1);
使用16.4 MB RAM
2.
尽量避免产生大的瞬时变量,当它们不用的时候应该及时clear
3.
尽量的重复使用变量(跟不用的clear掉一个意思)
4.
将矩阵转化成稀疏形式
如果矩阵中有大量的0,最好存储成稀疏形式。稀疏形式的矩阵使用内存更少,执行时间更短。
例如:
1000×1000的矩阵X,它2/3的元素为0,使用两种存储方法的比较:

Name
Size
Bytes
Class


X
1000x1000
8000000
double array


Y
1000x1000
4004000
double array (sparse)

5.
使用pack命令
当内存被分为很多碎片以后,其实本身可能有很大的空间,只是没有作构的连续空间即大的Block而已。如果此时Out of Memory,此时使用pack命令可以很好的解决此问题。
6.
如果可行的话,将一个大的矩阵划分为几个小的矩阵,这样每一次使用的内存减少。
7.
增大内存
问题三: Increase the size of the swap file.
wap space的设置与使用的操作系统有关,具体的如下:
1.
UNIX

Information about swap space can be procured by typing pstat -s at the UNIX command prompt. For detailed information on changing swap space, ask your system administrator.
2.
Linux

Swap space can be changed by using the mkswap and swapon commands. For more information on the above commands, type man followed by the command name at the Linux prompt.
3.
Windows NT

Follow the steps shown here: Right-click the My Computer icon, and select Properties. Select the Performance tab and click the Change button to change the amount of virtual memory.
4.
Windows 2000

右键“我的电脑”->属性->高级->性能->设置,从而改变其虚拟内存。
5.
Windows XP

右键“我的电脑”->属性->高级->性能->设置,从而改变其虚拟内存。
问题四:尽量少时用系统资源(对于Windows
Windows中字体、窗口等都是要占用系统资源的,所以在Matlab运行时尽量不要打开不用的窗口。
问题五:Reloading Variables on UNIX Systems
由于我不使用UNIX系统,这里不介绍,直接从Help中粘过来
On UNIX systems, MATLAB does not return memory to the operating system even after variables have been cleared. This is due to the manner in which UNIX manages memory. UNIX does not accept memory back from a program until the program has terminated. So, the amount of memory used in a MATLAB session is not returned to the operating system until you exit MATLAB.
To free up the memory used in your MATLAB session, save your workspace variables, exit MATLAB, and then load your variables back in.

36 条回复


pt_job 发表于 2008-12-5 08:52:59
谢谢,学到了不少东西

liuwudang 发表于 2008-12-13 11:17:56
好贴呀。顶一个

isee 发表于 2010-4-12 11:04:53
非常感谢,真的学到不少东西

oliver55 发表于 2010-7-5 19:05:46
好贴啊,好东西

zhizi123410 发表于 2010-7-5 19:11:04
真是好帖子啊

cumt蝈蝈 发表于 2011-1-19 18:33:12

回复 1# wangjuan313 的帖子

多谢分享,辛苦了!

qhw490 发表于 2011-7-27 09:25:53
learn much!! thanks!!

twqylsf 发表于 2011-7-27 19:07:40
好贴啊,好东西

dengxin2008 发表于 2012-3-18 10:51:15
学习了,谢谢!

bingle 发表于 2012-4-1 08:57:40
我的机子刚开MATLAB只运行了三句话:
syms x t;
y=(1+t/(2*x))^(4*x);
limit(y, x, inf)
然后就提示out of memory,
怎么办? 是不是只有换内存条

yichunlei1986 发表于 2012-4-15 12:18:01
顶啊,哈哈

richyhuang 发表于 2012-6-27 14:46:10
我觉的还有其他的原因,有时候出现out of memory很奇怪,我把仿真时间设为20s,仿真到最后的时候出现out of memory,我把仿真时间设成2s,也是在仿真最后出现。这个又如何解释呢。

bhylc 发表于 2012-7-28 22:05:00
谢谢啦 好东西

bamajiedi501 发表于 2012-8-9 17:00:54
好贴,谢谢楼主

phoenixlwei 发表于 2012-9-7 08:11:06
我去看看help

slnjnu 发表于 2012-9-9 17:38:54
正好遇到这样的问题~谢谢了

buxiandeyan 发表于 2012-10-10 12:05:18
学习了,呵呵

suoyifei 发表于 2012-10-27 19:25:10
出现了一样的问题,太有用了,谢谢

zhoushuai123 发表于 2012-12-8 22:29:11
学习了,感谢原创!

cj403522231 发表于 2013-3-3 17:02:45
谢谢楼主,学到了一些东西,但是对于大矩阵存储还是没解决

tigerzlp 发表于 2013-5-9 12:35:07
读完受益匪浅,特地登录回帖表示感谢~

长沟流月H 发表于 2013-5-14 16:58:02
真的很好,这位仁兄真细心啊

爱问功夫 发表于 2013-5-14 17:05:09
楼主真是有心人啊!遇到过这样的问题,但没有这么系统的思考过!

elninozz 发表于 2013-5-28 10:25:54
谢谢 大师 分享~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

相关帖子
相关文章
热门教程
站长推荐
快速回复 返回顶部 返回列表