MATLAB中数据的大小与内存的关系

MATLAB中数据的大小与内存的关系

今天跑程序出现:out of memory 的情况,发现原来受限于内存,矩阵等数据的大小是有限制的的。

输入:memory 查看发现:

 

memory
Maximum possible array:               5417 MB (5.680e+009 bytes) *
Memory available for all arrays:      5417 MB (5.680e+009 bytes) *
Memory used by MATLAB:                 643 MB (6.747e+008 bytes)
Physical Memory (RAM):                4063 MB (4.261e+009 bytes)

*  Limited by System Memory (physical + swap file) available.

 

现在 zeros(52200,52200)的数据是存不了的!

是不是将虚拟内存更改一下能够解决这个问题呢?

试探性的修改了一下虚拟内存:

 memory
Maximum possible array:               6483 MB (6.798e+009 bytes) *
Memory available for all arrays:      6483 MB (6.798e+009 bytes) *
Memory used by MATLAB:                 483 MB (5.065e+008 bytes)
Physical Memory (RAM):                4063 MB (4.261e+009 bytes)

*  Limited by System Memory (physical + swap file) available.
>> zeros(50000,50000);
??? Out of memory. Type HELP MEMORY for your options.
 
>>

 

memory
Maximum possible array:              12564 MB (1.317e+010 bytes) *
Memory available for all arrays:     12564 MB (1.317e+010 bytes) *
Memory used by MATLAB:                 479 MB (5.026e+008 bytes)
Physical Memory (RAM):                4063 MB (4.261e+009 bytes)

*  Limited by System Memory (physical + swap file) available.

 

虚拟内存达到一定程度对系统的运行速度就有非常明显的影响了,系统速度变得非常慢!!!

 

有什么其他方法?

参考一下博友:

http://blog.sina.com.cn/s/blog_4b892b790100m4hd.html

问题一:Matlab是如何存储矩阵的
    Matlab中矩阵是以Block,也就是块的形式存储的。也就是说,当Matlab在为即将存储的矩阵划分块时,如果没有相应大小的连续内存,即使实际内存没有被完全使用,他还是会报告“Out of Memory”。

问题二:如何高效使用Memory
    由于在使用的过程中,由于存储单元的不断的被分配和清除,内存会被分割成不连续的区域,这是很容易造成“Out of Memory”。

1.为矩阵变量预制内存而不是动态分配
    动态分配的过程中,由于开始Matlab所用的Block随着矩阵的增大而连续的为此矩阵分配内存,但是由于Block的不连续性,很有可能最开始分配的Block不能满足存储的需要,Matlab只好移动此Block以找到更大的Block来存储,这样在移动的过程中不但占用了大量的时间,而且很有可能它找不到更大的块,导致Out of Memory。而当你为矩阵变量预制内存时,Matlab会在计算开始前一次性找到最合适的Block,此时就不用为变量连续的分配内存。
    比较下面两个程序:

for k = 2:1000

x(k) = x(k-1) + 5;
end

x = zeros(1, 1000);
for k = 2:1000

x(k) = x(k-1) + 5;
end


 

    显然,第二个更好!!!最好的方法是,在程序一开始就位所有大的矩阵变量预制存存储单元!!!
    尽量早的分配大的矩阵变量

    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不能使用a、b被clear的空间,因为它们均小于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          X                   Y

Size      1000x1000           1000x1000

Bytes       8000000             4004000

Class    double array      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
        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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值