Main Content

pack

Consolidate workspace memory

Description

example

pack reorganizes workspace data to maximize the size of the largest contiguous free memory block. It consolidates fragmented memory by:

  • Saving all variables in the base and function workspaces to a temporary file

  • Clearing all variables and functions from memory

  • Reloading the base and function workspace variables back from the temporary file and then deleting the file

Note

When MATLAB® runs out of memory on a 32-bit system, pack might find you some free memory without forcing you to delete variables. On a 64-bit system, you do not need to use pack. For more information about strategies for reducing memory usage, see Strategies for Efficient Use of Memory and Resolve “Out of Memory” Errors.

pack filename frees space in memory, temporarily storing workspace data in a file specified by filename.

Examples

collapse all

Consolidate memory by running pack in a writeable folder.

Change the current folder to the temporary folder for the system, run pack, and then return to the previous folder.

cwd = pwd;
cd(tempdir)
pack
cd(cwd)

Input Arguments

collapse all

Name of file, specified as a character vector or string scalar. The file must be in a folder for which you have write permission. If you do not specify filename, pack temporarily stores your workspace data in a file in your temporary folder.

When using the command form of pack, you do not need to enclose the input in single quotes. However, if filename contains a space, you must enclose the argument in single quotes; for example, pack 'filename withspace.mat'.

Do not use command form when filename is a string. Use function form instead.

Limitations

  • The pack function is supported only in the Command Window. You cannot call it from a function or script.

  • If the workspace contains a variable larger than 2 GB, pack cannot save and reload your data.

  • The pack function does not affect the amount of memory allocated to the MATLAB process. You must end your MATLAB session to free up this memory.

Tips

  • The pack function initializes all persistent variables to an empty matrix ([]). To maintain the value of persistent variables when you run pack, use mlock in the functions in which they are declared.

  • Do not use the pack function with objects that define events and listeners. pack causes the destruction of any listeners defined for the objects in the workspace. For information about restoring listeners when saving objects, see Restore Listeners.

Introduced before R2006a