MATLAB: Memory used vs RAM

memory used vs ram

Hi!
I'm using large arrays and the "Memory used by Matlab" is slightly bigger than the RAM of my PC (4100 MB vs 3900). The program is very slow. Does this mean that Matlab uses the Hard Disk Drive to calculate?
Moreover (it's a more general question), why is my whole computer very slow in the minutes after stopping this program (though it didn't crash) using CTRL+C?
Finally, the memory used by Matlab (1 GB, according to the task manager) is far bigger than when I first launched it. Why? It is probably the same question/answer as above actually…
Thanks for your answers,
Martin

Best Answer

Yes, if the Memory Being Used is shown as higher than your RAM, your system must be allocating virtual memory, probably from a hard disk. That can be very slow, and it usually gets slower as more memory is swapped to hard disk.
When a lot of memory is being swapped to hard disk, the system on the whole can get quite slow. When you press control-C, because there is no way to throw/catch a control-C, any functions in the calling stack and all non-persistent memory in the workspace (there there is no other reference to) must be released, which probably involves bringing that variable back into active memory again (forcing swaps.)
If you see that memory used by MATLAB has increased even when you are at the command line (no routines on the call stack), there are some possible reasons:
  • internal buffers may have been allocated, such as for Data Acquisition
  • memory might have become fragmented enough that MATLAB cannot figure out that there are adjacent chunks that could be put together again
  • You might have used global variables, as those are not freed when the functions terminate
  • You might have created a bunch of graphics objects that have not been cleared out yet. If you used "hold on" and did not notice that you were covering over old objects, then the memory allocated for graphics can grow large
  • You might have Objects associated with callback routines associated with objects that have not been cleared (such as timers)
  • If I recall correctly, MATLAB uses (or used to) a dual memory allocation strategy, in which memory requests under a particular size are handled out of fix-length chunks of memory, and memory requests over that size are handled by allocating as much memory as is needed to fit. When one of the large chunks is no longer used, MATLAB logically gives it back to the operating system (on MS Windows), but when one of the fixed-sized blocks is no longer used, MATLAB holds on to it, under the assumption that you are probably going to need another variable the same size in another few microseconds. Because of this strategy, if you use a lot of small values, the amount of memory that MATLAB is holding on to for later use can continue to grow
Related Question