Resolve “Out of Memory” Errors

General Suggestions for Reclaiming Memory

The MATLAB® software is a 64-bit application that runs on 64-bit operating systems. It generates an Out of Memory message whenever it requests a segment of memory from the operating system that is larger than what is available. When you see the Out of Memory message, use any of the techniques discussed under Strategies for Efficient Use of Memory to help optimize the available memory including:

  • Reducing required memory

  • Selecting appropriate data storage

  • Using contiguous memory

  • Reclaiming used memory

If the Out of Memory message still appears, you can try any of the following:

  • If possible, reduce the size of your data. For example, break large matrices into several smaller matrices so that less memory is used at any one time.

  • If you have large files and data sets, see Large Files and Big Data.

  • Make sure that there are no external constraints on the memory accessible to MATLAB. On Linux® systems, use the limit command to investigate.

  • Increase the size of the swap file. We recommend that you configure your system with twice as much swap space as you have RAM. For more information, see Increase System Swap Space.

  • Add more memory to the system.

Increase System Swap Space

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.

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.

Set the Process Limit on Linux Systems

The process limit is the maximum amount of virtual memory a single process (or application) can address. The process limit must be large enough to accommodate:

  • All the data to process

  • MATLAB program files

  • The MATLAB executable itself

  • Additional state information

The 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.

Disable Java VM on Linux Systems

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. 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.

Free System Resources on Windows Systems

There are no MATLAB functions to manipulate the way MATLAB handles Microsoft® Windows system resources. Windows systems use these resources to track fonts, windows, and screen objects. For example, using multiple figure windows, multiple fonts, or several UI controls can deplete resources. One way to free up system resources is to close all inactive windows. Windows system icons still use resources.

If total system memory is the limiting factor, shutting down other applications and services can help (for example, using msconfig on Windows systems). However, the process limit is usually the main limiting factor.

See Also

Related Topics