When I run computationally-intensive scripts, memory usage goes up and my computer slows down a little bit. This is to be expected. But sometimes after running one of these scripts I close all and clear all, yet when I check my system Activity Monitor Matlab is still using 3.5 GB and my computer runs slow until I restart Matlab. Any tricks for clearing up memory without restarting Matlab? I'm running Matlab R2012b on OSX 10.8.5.
MATLAB: How to clear up some memory
MATLABmemoryram
Related Solutions
In theory, 32-bit applications can address up to 4GB of virtual memory.
However, a number of things affect the actual memory accessibility:
1. The Operating System reserves memory for itself, reducing the amount of memory its applications can use.
2. Other applications running at the same time can eat up memory as well.
3. MATLAB reserves memory for itself. Even when there is no code running and there are no variables in the workspace, MATLAB is still using memory.
4. The memory available to MATLAB may be fragmented when starting additional tools or toolboxes, or loading MATLAB functions into memory. This limits the size of the largest contiguous block of memory. Since MATLAB matrices need to be stored in contiguous blocks, Out of Memory errors can occur while trying to create a matrix larger than the size of the largest contiguous block, even if that memory exists in several fragmented blocks.
There are tools for memory management for Macs:
The TOP command allows you to view the RPRVT (amount of memory in use) and VSIZE(amount of virtual memory paged so far) for the applications with top CPU usage:
top
To access more memory you can run MATLAB from xterm with –nojvm option:
matlab –nojvm
which does not load Java Virtual Machine and thus saves memory MATLAB uses for itself otherwise.
For more on Memory Management, please refer to the following Tech Notes:
To add to what Daniel has already written, consider these lines:
function obj=lu_apply(A) [L,U]=lu(A); obj.f=@(X) U\(L\X); end
The L and U matrices get created inside the function lu_apply. They are local to the function and will get destroyed once the function returns. The following line:
obj.f=@(X) U\(L\X);
creates a function handle using the local L and U variables as part of the function definition. At that time, shared data copies of L and U are created and become embedded in the data area of the function handle ... i.e., they essentially become part of the function handle variable itself. Basically these shared data copies of L and U become snapshots of the current state of L and U at the time of the function handle creation and will remain as constants as far as the function handle is concerned.
Whatever happens to L and U downstream doesn't affect this function handle anymore. In particular, when the function returns, the local variables L and U are destroyed, but the shared data copies of them that were created earlier still live on (and use memory) as part of the function handle. The only way to clear this memory is to clear the function handle itself. I don't know of any way to directly interrogate a function handle to determine the sizes of the embedded variables it contains, since I am not familiar with how function handle definitions are encoded in the data area of the variable.
Best Answer