Strategies for Efficient Use of Memory - MATLAB & Simulink - MathWorks 中国

Strategies for Efficient Use of Memory

Ways to Reduce the Amount of Memory Required

The source of many "out of memory" problems often involves analyzing or processing an existing large set of data such as in a file or a database. This requires bringing all or part of the data set into the MATLAB® software process. The following techniques deal with minimizing the required memory during this stage.

Load Only As Much Data As You Need

Only import into MATLAB as much of a large data set as you need for the problem you are trying to solve. This is not usually a problem when importing from sources such as a database, where you can explicitly search for elements matching a query. But this is a common problem with loading large flat text or binary files. Rather than loading the entire file, use the appropriate MATLAB function to load parts of files.

MAT-Files.Load part of a variable by indexing into an object that you create with the matfile function.

Text Files.Use the textscan function to access parts of a large text file by reading only the selected columns and rows. If you specify the number of rows or a repeat format number with textscan, MATLAB calculates the exact amount of memory required beforehand.

Binary Files.You can use low-level binary file I/O functions, such as fread, to access parts of any file that has a known format. For binary files of an unknown format, try using memory mapping with the memmapfile function.

Image, HDF, Audio, and Video Files.Many of the MATLAB functions that support loading from these types of files allow you to select portions of the data to read. For details, see the function reference pages listed in Supported File Formats for Import and Export.

Process Data By Blocks

Consider block processing, that is, processing a large data set one section at a time in a loop. Reducing the size of the largest array in a data set reduces the size of any copies or temporaries needed. You can use this technique in either of two ways:

For a subset of applications that you can break into separate chunks and process independently.

For applications that only rely on the state of a previous block, such as filtering.

Avoid Creating Temporary Arrays

Avoid creating large temporary variables, and also make it a practice to clear those temporary variables you do use when they are no longer needed. For example, when you create a large array of zeros, instead of saving to a temporary variable A, and then converting A to a single:

A = zeros(1e6,1);

As = single(A);

use just the one command to do both operations:

A = zeros(1e6,1,'single');

Using the repmat function, array preallocation and for loops are other ways to work on nondouble data without requiring temporary storage in memory.

Use Nested Functions to Pass Fewer Arguments

When working with large data sets, be aware that MATLAB makes a temporary copy of an input variable if the called function modifies its value. This temporarily doubles the memory required to store the array, which causes MATLAB to generate an error if sufficient memory is not available.

One way to use less memory in this situation is to use nested functions. A nested function shares the workspace of all outer functions, giving the nested function access to data outside of its usual scope. In the example shown here, nested function setrowval has direct access to the workspace of the outer function myfun, making it unnecessary to pass a copy of the variable in the function call. When setrowval modifies the value of A, it modifies it in the workspace of the calling function. There is no need to use additional memory to hold a separate array for the function being called, and there also is no need to return the modified value of A:

function myfun

A = magic(500);

function setrowval(row, value)

A(row,:) = value;

end

setrowval(400, 0);

disp('The new value of A(399:401,1:10) is')

A(399:401,1:10)

end

Using Appropriate Data Storage

MATLAB provides you with different sizes of data classes, such as double and uint8, so you do not need to use large classes to store your smaller segments of data. For example, it takes 7 KB less memory to store 1,000 small unsigned integer values using the uint8 class than it does with double.

Use the Appropriate Numeric Class

The numeric class you should use in MATLAB depends on your intended actions. The default class double gives the best precision, but requires 8 bytes per element of memory to store. If you intend to perform complicated math such as linear algebra, you must use a floating-point class such as a double or single. The single class requires only 4 bytes. There are some limitations on what you can do with the single class, but most MATLAB Math operations are supported.

If you just need to carry out simple arithmetic and you represent the original data as integers, you can use the integer classes in MATLAB. The following is a list of numeric classes, memory requirements (in bytes), and the supported operations.

Class (Data Type)

Bytes

Supported Operations

single

4

Most math

double

8

All math

logical

1

Logical/conditional operations

int8, uint8

1

Arithmetic and some simple functions

int16, uint16

2

Arithmetic and some simple functions

int32, uint32

4

Arithmetic and some simple functions

int64, int64

8

Arithmetic and some simple functions

Reduce the Amount of Overhead When Storing Data

MATLAB arrays (implemented internally as mxArrays) require room to store meta information about the data in memory, such as type, dimensions, and attributes. This takes about 80 bytes per array. This overhead only becomes an issue when you have a large number (e.g., hundreds or thousands) of small mxArrays (e.g., scalars). The whos command lists the memory used by variables, but does not include this overhead.

Because simple numeric arrays (comprising one mxArray) have the least overhead, you should use them wherever possible. When data is too complex to store in a simple array (or matrix), you can use other data structures.

Cell arrays are comprised of separate mxArrays for each element. As a result, cell arrays with many small elements have a large overhead.

Structures require a similar amount of overhead per field (see Array Headers). Structures with many fields and small contents have a large overhead  and should be avoided. A large array of structures with numeric scalar fields requires much more memory than a structure with fields containing large numeric arrays.

Also note that while MATLAB stores numeric arrays in contiguous memory, this is not the case for structures and cell arrays.

Import Data to the Appropriate MATLAB Class

When reading data from a binary file with fread, it is a common error to specify only the class of the data in the file, and not the class of the data MATLAB uses once it is in the workspace. As a result, the default double is used even if you are reading only 8-bit values. For example,

fid = fopen('large_file_of_uint8s.bin', 'r');

a = fread(fid, 1e3, 'uint8');              % Requires 8k

whos a

Name         Size            Bytes  Class    Attributes

a         1000x1              8000  double

a = fread(fid, 1e3, 'uint8=>uint8');       % Requires 1k

whos a

Name         Size            Bytes  Class    Attributes

a         1000x1              1000  uint8

Make Arrays Sparse When Possible

If your data contains many zeros, consider using sparse arrays, which store only nonzero elements. The following example compares the space required for storage of an array of mainly zeros:

A = eye(1000);        % Full matrix with ones on the diagonal

As = sparse(A);       % Sparse matrix with only nonzero elements

whos

Name         Size                Bytes  Class     Attributes

A         1000x1000            8000000  double

As        1000x1000              24008  double    sparse

You can see that this array requires only approximately 4 KB to be stored as sparse, but approximately 8 MB as a full matrix. In general, for a sparse double array with nnz nonzero elements and ncol columns, the memory required is

16 * nnz + 8 * ncol + 8 bytes (on a 64-bit machine)

12 * nnz + 4 * ncol + 4 bytes  (on a 32-bit machine)

Note that MATLAB does not support all mathematical operations on sparse arrays.

How to Avoid Fragmenting Memory

MATLAB always uses a contiguous segment of memory to store a numeric array. As you manipulate this data, however, the contiguous block can become fragmented. When memory is fragmented, there might be plenty of free space, but not enough contiguous memory to store a new large variable. Increasing fragmentation can use significantly more memory than is necessary.

Preallocate Contiguous Memory When Creating Arrays

In the course of a MATLAB session, memory can become fragmented due to dynamic memory allocation and deallocation. for and while loops that incrementally increase, or grow, the size of a data structure each time through the loop can add to this fragmentation as they have to repeatedly find and allocate larger blocks of memory to store the data.

To make more efficient use of your memory, preallocate a block of memory large enough to hold the matrix at its final size before entering the loop. When you preallocate memory for an array, MATLAB reserves sufficient contiguous space for the entire full-size array at the beginning of the computation. Once you have this space, you can add elements to the array without having to continually allocate new space for it in memory.

For more information on preallocation, see Preallocation.

Allocate Your Larger Arrays First

MATLAB uses a heap method of memory management. It requests memory from the operating system when there is not enough memory available in the heap to store the current variables. It reuses memory as long as the size of the memory segment required is available in the heap.

The following statements can require approximately 4.3 MB of RAM. This is because MATLAB might not be able to reuse the space previously occupied by two 1 MB arrays when allocating space for a 2.3 MB array:

a = rand(1e6,1);

b = rand(1e6,1);

clear

c = rand(2.3e6,1);

The simplest way to prevent overallocation of memory is to allocate the largest vectors first. These statements require only about 2.0 MB of RAM:

c = rand(2.3e6,1);

clear

a = rand(1e6,1);

b = rand(1e6,1);

Long-Term Usage (Windows Systems Only)

On 32-bit Microsoft® Windows®, the workspace of MATLAB can fragment over time due to the fact that the Windows memory manager does not return blocks of certain types and sizes to the operating system. Clearing the MATLAB workspace does not fix this problem. You can minimize the problem by allocating the largest variables first. This cannot address, however, the eventual fragmentation of the workspace that occurs from continual use of MATLAB over many days and weeks, for example. The only solution to this is to save your work and restart MATLAB.

The pack command, which saves all variables to disk and loads them back, does not help with this situation.

Reclaiming Used Memory

One simple way to increase the amount of memory you have available is to clear large arrays that you no longer use.

Save Your Large Data Periodically to Disk

If your program generates very large amounts of data, consider writing the data to disk periodically. After saving that portion of the data, use the clear function to remove the variable from memory and continue with the data generation.

Clear Old Variables from Memory When No Longer Needed

When you are working with a very large data set repeatedly or interactively, clear the old variable first to make space for the new variable. Otherwise, MATLAB requires temporary storage of equal size before overriding the variable.  For example,

a = rand(100e6,1)              % 800 MB array

b = rand(100e6,1)              % New 800 MB array

Error using rand

Out of memory. Type HELP MEMORY for your options.

clear a

a = rand(100e6,1)              % New 800 MB array

matlab需要多大运存_MATLAB下高效使用内存相关推荐

  1. matlab需要多大运存_提高matlab运行效率

    用过Matlab的人都知道,Matlab是一种解释性语言,存在计算速度慢的问题,为了提高程序的运行效率,matlab提供了多种实用工具及编码技巧. 1. 循环矢量化 Matlab是为矢量和矩阵操作而设 ...

  2. 最常用的10个Matlab快捷键,助你编程更高效

    本文转载:最常用的10个Matlab快捷键,助你编程更高效 目录 1. 屏蔽大段程序:Ctrl+r 2. 自动对齐程序:Ctrl+i 3. 直接跳至某行:Ctrl+g 4. 设置标签:Ctrl+F2 ...

  3. nova3安装android10系统,一招搞定安卓机“久用必卡”顽疾:华为nova3i配备6GB超大运存...

    [TechWeb]现在手机上安装的APP占用的内存空间是越来越大,特别是安卓手机如果长时间使用后,小内存必然会影响流畅的运行速度,虽说厂商一直在竭尽全力通过"杀后台"方式进行内存优 ...

  4. matlab中调用java代码_Matlab中调用第三方Java代码

    在Java中采用Matlab JA Builder可以实现调用m文件,采用这样的方式,可在Matlab的M文件中,直接调用Java类.这种方式可以表示为Java--> Matlab( m, Ja ...

  5. matlab连接mysql教程视频_Matlab建立到Oracle数据库的连接

    Linux下的配置过程和Windows一样,如下:1.将Oracle JDBC的JAR包拷贝到Matlab的相关目录(..\matlab\java\jar\toolbox\)下. 一.Matlab通过 ...

  6. Mac OSX 下高效安装 homebrew 及完美避坑姿势

    Mac OSX 下高效安装 homebrew 及完美避坑姿势 Homebrew 是什么 Homebrew是 mac的包管理器,仅需执行相应的命令,就能下载安装需要的软件包,可以省掉自己去下载.解压.拖 ...

  7. GaussDB(for openGauss)让数据“存得下、算得快、算得准”

    本文分享自华为云社区<华为云GaussDB(for openGauss)专场直播第2期:让数据"存得下.算得快.算得准">,原文作者:心机胖 . 1.前言 随着云计算规 ...

  8. 基带信号及其眼图MATLAB仿真实现,Matlab通信仿真——带限系统下的基带信号

    Matlab通信仿真--带限系统下的基带信号 Matlab通信仿真--带限系统下的基带信号 1 余弦滚降特性 满足消除码间串扰条件的H(f)有很多种,容易想到的一种极限情况,就是H(f)为理想低通型. ...

  9. matlab 眼图 值,Matlab通信仿真——带限系统下的基带信号

    Matlab通信仿真--带限系统下的基带信号 Matlab通信仿真--带限系统下的基带信号 1 余弦滚降特性 满足消除码间串扰条件的H(f)有很多种,容易想到的一种极限情况,就是H(f)为理想低通型. ...

  10. 鸿蒙系统需要大运存吗,荣耀5G最新确认,鸿蒙系统+双5000万+16GB大运存,这才是荣耀...

    原标题:荣耀5G最新确认,鸿蒙系统+双5000万+16GB大运存,这才是荣耀 荣耀上半年发布完荣耀30pro后,很多荣耀的粉丝就开始期待下半年的荣耀5G新机,大多数消费者都在推测荣耀Magic 3的上 ...

最新文章

  1. 强烈推荐16 款牛逼的 IDEA 插件,让你开发速度飞起来!
  2. ECSHOP商品编辑器上传中文名图片产生乱码
  3. c# is和as的区别
  4. 自动滚放的html,HTML5实现视频播放器随页面滚动固定页面右下角效果详解
  5. 三种权重的初始化方法
  6. 计算机操作鉴定所需设备,计算机操作员职业技能鉴定标准(高级)
  7. python 元类的call总结_Python 类与元类的深度挖掘 I【经验】
  8. 全数字实时仿真平台SkyEye的同步数据流语言可信编译器的构造
  9. [jQuery]超出容器部分...
  10. 手写字体识别(1) 准备数据集
  11. ADF4351原理图PCB电路设计经验建议
  12. vue中views新建文件夹的代码规范
  13. html 分页样式首页下一页,css中分页样式怎么设置
  14. 软件架构设计-大型网站技术架构于业务架构融合之道——部分知识点总结【未完】
  15. 耗油是什么,怎么用?
  16. 程序猿生存指南-53 春日凉亭
  17. PAT-B 1036. 跟奥巴马一起编程(15)(15 分) 画方型字符
  18. adb wifi 连接设备
  19. vi之列操作——步步为营+实例
  20. 好的设计要多分享,5款优秀在线原型设计案例

热门文章

  1. 【技术讨论】从弹弹堂说起,如何用2D物理引擎编写一个游戏lt;一gt;2011-11-05 10:36
  2. Python beautiful soup解析html获得数据
  3. EasyExcel 固定(冻结)单元格
  4. linux系统scsi硬盘,Linux系统中SCSI硬盘的热拔插
  5. python输出欢迎某某某_python中怎么写注释
  6. 《Evolutionary Computation for Expensive Optimization:A Survey》笔记
  7. 计算机辅助设计实训报告范文,计算机辅助设计实习实习报告
  8. win7电脑桌面背景异常
  9. 全球首款基于开放式工业控制系统的EdgeIO边缘计算IO模块诞生
  10. java生成N位随机数字