This topic explains several strategies you can use in situations where MATLAB® runs out of memory. MATLAB is a 64-bit application that runs on 64-bit operating systems. It returns an error message whenever it requests a segment of memory from the operating system that is larger than what is available.
MATLAB has built-in protection against creating arrays that are too large. By default, MATLAB can use up to 100% of the RAM (not including virtual memory) of your computer to allocate memory for arrays, and if an array would exceed that threshold, then MATLAB returns an error. For example, this statement attempts to create an array with an unreasonable size:
A = rand(1e6,1e6);
Error using rand Requested 1000000x1000000 (7450.6GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive. More information
A = rand(1e6,1e6);
Out of memory. More information
No matter how you run into memory limits, there are several resolutions available depending on your goals. The techniques discussed in Strategies for Efficient Use of Memory can help you optimize the available memory you have, including:
If you are already using memory efficiently and the problem persists, then the remaining sections of this page contain possible solutions.
tall
ArraysTall Arrays for Out-of-Memory Data are designed to help you work with data sets that are too large to fit into memory. MATLAB works with small blocks of the data at a time, automatically handling all of the data chunking and processing in the background. There are two primary ways you can leverage tall arrays:
If you have a large array that fits in memory, but you run out of memory when you try to perform calculations, you can cast the array to a tall array:
B = tall(A)
If you have file or folder-based data, you can create a datastore
and then create a tall array on top of the datastore:
ds = datastore('path/to/data.csv');
tt = tall(ds);
datastore
works with both local and remote data locations, the data you work with does not need to be on the computer you use to analyze it. See Work with Remote Data for more information.
If you have a cluster of computers, you can use the Parallel Computing Toolbox™ and Distributed Arrays (Parallel Computing Toolbox) to perform calculations using the combined memory of all the machines in the cluster. This enables you to operate on the entire distributed array as a single entity. However, the workers operate only on their part of the array, and automatically transfer data between themselves when necessary.
Creating a distributed array is very similar to creating a tall array:
ds = datastore('path/to/data.csv');
dt = distributed(ds);
Another possible way to fix memory issues is to 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.
The datastore
function enables you to work with large data sets incrementally. This function underpins Tall Arrays for Out-of-Memory Data and Distributed Arrays (Parallel Computing Toolbox), but you can use it for other purposes as well. Datastores are useful any time you want to load small portions of a data set into memory at a time.
To create a datastore, you need to provide the name of a file or a directory containing a collection of files with similar formatting. For example, with a single file:
ds = datastore('path/to/file.csv')
ds = datastore('path/to/folder/')
*
to select all files of a specific type, as in:ds = datastore('data/*.csv')
Aside from datastores, MATLAB also has several other functions to load parts of files, such as the matfile
function to load portions of MAT-files. This table summarizes partial loading functions by file type.
File Type | Partial Loading |
---|---|
MAT-file | Load part of a variable by indexing into an object that you create with the |
Text | Use the |
Binary | You can use low-level binary file I/O functions, such as |
Image, HDF, Audio, and Video | 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. |
The total memory available to applications on your computer is composed of physical memory (RAM), plus a page file, or swap file, on disk. The swap file can be very large (for example, 512 terabytes on 64-bit Windows®). The operating system allocates the virtual memory for each process to physical memory or to the swap file, depending on the needs of the system and other processes. Increasing the size of the swap file can increase the total available memory, but also typically leads to slower performance.
Most systems enable you to control the size of your swap file. The steps involved depend on your operating system:
Windows Systems — Use the Windows Control Panel to change the size of the virtual memory paging file on your system. For more information, refer to the Windows help.
Linux® Systems — Change your swap space by using the mkswap
and swapon
commands. For more information, at the Linux prompt type man
followed by the command name.
There is no interface for directly controlling the swap space on macOS systems.
The process limit is the maximum amount of virtual memory a single process (or application) can address. In the unlikely case you have set this preference, it must be large enough to accommodate:
All the data to process
MATLAB program files
The MATLAB executable itself
Additional state information
64-bit operating systems support a process limit of 8 terabytes. On Linux systems, see the ulimit
command to view and set user limits including virtual memory.
On Linux systems, if you start MATLAB without the Java®
JVM™, you can increase the available workspace memory by approximately 400 megabytes. To start MATLAB without Java
JVM, use the command-line option -nojvm
. This option also increases the size of the largest contiguous memory block by about the same amount. By increasing the largest contiguous memory block, you increase the largest possible matrix size.
Using -nojvm
comes with a penalty in that you lose many features that rely on the Java software, including the entire development environment. Starting MATLAB with the -nodesktop
option does not save any substantial amount of memory.