Documentation Center

  • Trial Software
  • Product Updates

Memory Management Functions

The following functions can help you to manage memory use while running the MATLAB® software:

  • memory displays or returns information about how much memory is available and how much is used by MATLAB. This includes the following:

    • Size of the largest single array MATLAB can create at this time.

    • Total size of the virtual address space available for data.

    • Total amount of memory used by the MATLAB process for both libraries and data.

    • Available and total Virtual Memory for the MATLAB software process.

    • Available system memory, including both physical memory and paging file.

    • Available and the total physical memory (RAM) of the computer.

  • whos shows how much memory MATLAB currently 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.

    If you use pack and there is still not enough free memory to proceed, you probably need to remove some of the variables you are no longer using from memory. Use clear to do this.

  • 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 (e.g., MATLAB) until the application exits.

You can use the save and load functions in conjunction with the quit command to free memory by:

  1. Saving any needed variables with the save function.

  2. Quitting MATLAB to free all memory allocated to MATLAB.

  3. Starting a new MATLAB session and loading the saved variables back into the clean MATLAB workspace.

The whos Function

The whos command can give you an idea of the memory used by MATLAB variables.

A = ones(10,10);
whos
   Name       Size            Bytes  Class     Attributes
   A         10x10              800  double 

Note that whos does not include information about

  • Memory used by MATLAB (for example, Java® code and plots).

  • Memory used for most objects (e.g., time series, custom) .

  • Memory for variables not in the calling workspace .

  • Shared data copies. whos shows bytes used for a shared data copy even when it does not use any memory. This example shows that whos reports an array (A) and a shared data copy of that array (B) separately, even though the data exists only once in memory:

    Store 400 MB array as A. Memory used = 381MB (715 MB – 334 MB) :

    memory
    Memory used by MATLAB:              334 MB (3.502e+008 bytes)
    
    A = rand(5e7,1);
    
    memory
    Memory used by MATLAB:              715 MB (7.502e+008 bytes)
    
    whos
      Name           Size            Bytes  Class     Attributes
    
      A       50000000x1         400000000  double              
    

    Create B and point it to A. Note that although whos shows both A and B, there is only one copy of the data in memory. No additional memory is consumed by assigning A to B:

    B = A;
    
    memory
    Memory used by MATLAB:              715 MB (7.502e+008 bytes)
    
    whos
      Name           Size            Bytes  Class     Attributes
    
      A       50000000x1         400000000  double
      B       50000000x1         400000000  double

    Modifying B(1)copies all of A to B and changes the value of B(1). Memory used = 382MB (1097 MB – 715 MB). Now there are two copies of the data in memory, yet the output of whos does not change:

    B(1) = 3;
    
    memory
    Memory used by MATLAB:             1097 MB (1.150e+009 bytes)
    
    whos
      Name           Size            Bytes  Class     Attributes
    
      A       50000000x1         400000000  double
      B       50000000x1         400000000  double
Was this topic helpful?