Open In App

How MATLAB Allocates Memory?

Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB is used to analyze and design the system and products transforming our world. MATLAB is a matrix-based language that is the most natural way to express Computational Mathematics. that’s Built-in graphics that make it easy to visualize and gain insights from data. 

The purpose of memory allocation is to inform about the internal operation like Arithmetic operations and many more those are running behind the MATLAB editor, basically that information is do helping to write efficient code in MATLAB. 

The Way of Creation and Modification Arrays:

  • Creating the virtual block of memory, where MATLAB allocates a contiguous virtual block of memory and stores the array data in that block.
  • Change the content of the array in the caller function through the calling function by passing the value of the array to the function.
  • A way that keeps its storage contiguous memory is through expanding MATLAB memory.
  • It can copy the data from original source location to the new block in memory(which we created earlier).
  • If we want to remove the element from an existing array, then MATLAB keeps the memory storage contiguous by removing the deleted element.

Memory Allocation with  Larger Data Sets:

So, let’s understand when we have larger data set an array then how will assign or allocate the memory in MATLAB? If we working with large data sets so we need to be careful when increasing the size of an array to avoid getting errors caused by insufficient memory. In this condition, large data set array is expanded beyond the available memory of its original location, so we need to ensure that MATLAB must take a copy of the array and replace with the new value. So during this operation, we have two types of copies of the original array in the memory. these two copy temporarily doubles the amount of memory required for the array and increases the risk of our program running out of memory during execution. So it is better and recommended to Pre-allocate sufficient memory for the largest potential size of the program itself. 

Copying Arrays:

Here we will see what goes behind the scene when you copy the array from one variable to another internally multiple variables can point the same block of data and can share the same array value. so suppose when A copy is made to another variable like B is equal to MATLAB makes a copy of the array reference but not of the array itself. But MATLAB makes a copy of the array and then it modifies that copy. 

 

In the above output getting the memory is being used by the user and by the MATLAB process, Here, currently running so using memory usage that’s why getting this information.

Now we will create 2000 by 2000 numeric array a using magic function that will approximately use around 32 MB of memory. so from 272 to memory is rises up to 327. some times you will get a change in memory.

 

we can also try and  following inputs:

B = A;
ans = 327.6349e+004 

B(1001 : 2000,:) = [];
format short;
size(B)
ans = 1000 2000

Function Arguments – 

It’s similar to a copying array when you pass a variable to a function you are actually passing reference to the data that represent the variable. so as long as the input data is not modified by the function the variable in the calling function and the variable in the called function point to some location in the memory.

Matlab




A = magic(500);
myfunc (A);
function myfun(X)
X(400,:) = 0;
 
%% Modified Argument
A = magic(500);
A = myfun(A);
sprintf('The new value of A is %d', A)
function Y = myfun(X)
X(400,:) = 0;
Y = X;


Data Structure and Memory:

In memory allocation, some come under the Data structure and memory.

  • Array Header and Cell Arrays: MATLAB also stores the information of class and dimensions of the arrays but that information is stored in a separate place of memory called an Arrays header. For most arrays, the memory required to store the header is basically insignificant. there is small advantage is we store a large number of arrays in larger array set.  For example, take a scalar specter array.
a  Array Header for Entire Data Structure
S1.R(1:100, 1:50)
S1.G(1:100, 1:50)
S1.B(1:100, 1:50)
  • Numeric Arrays: MATLAB requires 1,2,4 and 8 bytes and signed & unsigned bytes to store.
  • Complex Arrays:  MATLAB stores complex data as separate real and imaginary parts.
  • Sparse Matrices: It’s best to form store matrices with values that are mostly zero in sparse format.
whose
Name      Size         Bytes       Class
X        1000x1000   8000000     double array
Y        1000x1000   4004000     double array
sparse)

Memory Management Function:

So, for managing the memory function there is a certain functionality that allows us to manage the memory function in MATLAB. 

  1. Memory Function which displays or returns the status of memory like how much memory is available and how much memory is used via MATLAB.
  2. Clear Function, basically removes a  from the memory it also helps to increase the availability of the memory.
  3. The load Function, it’s used to reload the data file safely with the help of the save function.

For Memory Management there are many kinds of functions used to manage the Memory in MATLAB.



Last Updated : 31 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads