Matlab的内存分配

原创 2017年01月03日 12:56:38
%% Memory Allocation (Matlab的内存分配)
% Creating and Modifying Arrays
% When you assign a numeric or character array to a variable, 
% MATLAB allocates a contiguous virtual block of memory and stores the array data in that block.
% MATLAB also stores information about the array data, such as its class and dimensions, in a separate, 
% small block of memory called a header. 
% (有两部分,一部分内存分配给变量代表的数组的数据,另一部分储存有数组数据的信息(),例如:class and dimensions)
% Array Headers 对于大的数据集有用
 
% If you add new elements to an existing array, MATLAB expands the existing array in memory in a way that keeps its storage contiguous. 
% This usually requires finding a new block of memory large enough to hold the expanded array. 
% MATLAB then copies the contents of the array from its original location to this new block in memory,
% adds the new elements to the array in this block, and frees up the original array location in memory.
% If you are working with large data sets, you need to be careful when increasing the size of an array to avoid getting errors caused by insufficient memory
% 对于不是预先分配内存,临时添加一个元素,通常先得找一块大的内存(为了保证连续性),将值复制过去,再添加,然后释放掉原先的内存,这样的话很可能会出错

% If you remove elements from an existing array,
% MATLAB keeps the memory storage contiguous by removing the deleted elements, 
% and then compacting its storage in the original memory location.
% 删掉某个元素时,会在保证连续性的情况下,去除该元素,压缩对应内存

% b = a;只要不修改 b 那么 a b 共用一个地址,修改之后将 a 赋值给 b ,在修改 b 中的值
memory 
A = rand(5e7,1); % Store 400 MB array as A. Memory used = 381MB
% 内存增加 381 M
memory
B = A; % 内存不变
memory
B(1) = 3; % 内存增加 381 M
memory
%% Memory Management Functions
memory  % displays or returns information about how much memory is available and how much is used by MATLAB. 
whos % shows how much memory MATLAB has allocated for variables in the workspace.
pack  % saves existing variables to disk, and then reloads them contiguously. This reduces the chances of running into problems due to memory fragmentation.
clear % removes variables from memory. One way to increase the amount of available memory is to periodically clear variables from memory that you no longer need.
save % selectively stores variables to the disk. This is a useful technique when you are working with large amounts of data. Save data to the disk periodically, and then use the clear function to remove the saved data from memory.
load % reloads a data file saved with the save function.
quit % exits MATLAB and returns all allocated memory to the system. This can be useful on UNIX? systems, which do not free up memory allocated to an application (for example, MATLAB) until the application exits.

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Matlab 的内存分配机制

1、创建和修改数组——基本思想是保持内存的连续性 (1)当为数组赋值时,Matlab在内存中分配连续的虚拟块,在另一个地方,将数组的类型和维数等保存为头信息。 (2)当向数组增加新元素时,如果原来...

C++程序内存分配讲解

  • 2016-01-03 01:04
  • 40.60MB
  • 下载

android模拟内存分配

  • 2016-07-15 15:35
  • 20.84MB
  • 下载

C语言内存分配

C语言内存分配C语言的内存分配有静态内存以及动态动态,静态内存由系统分配并释放,动态内存由自己申请与释放.静态内存静态内存是由栈空间分配的,由系统自动分配,自动释放(随机释放)main() { ...

11_内存分配 视频

  • 2016-03-24 18:29
  • 22.49MB
  • 下载

动态内存分配

  • 2015-05-14 11:39
  • 150KB
  • 下载

Memcache内存分配机制

1. page为内存分配的最小单位 Memcached 的内存分配以page为单位,默认情况下一个page是1M,可以通过-I参数在启动时指定。如果需要申请内存时,memcached会划分出一个新...

物理内存分配

  • 2015-03-29 17:27
  • 13KB
  • 下载

Oracle内存分配与调整

  • 2014-09-11 12:52
  • 181KB
  • 下载

垃圾收集器和内存分配策略

刚刚在前面介绍了内存的运行时数据区域,其中程序计数器、虚拟机栈、本地方法栈3个区域随线程而生,随线程而灭。栈中的栈帧随着方法的进入和退出而有条不紊的执行着入栈和出栈的操作,每一个栈帧中分配多少内存是在...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)