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

 채택된 답변

Walter Roberson
Walter Roberson 2013년 7월 5일

2 개 추천

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

댓글 수: 5

Martin
Martin 2013년 7월 7일
Thanks for this great answer. Very interesting. I've got a few more questions:
When you say "any functions in the calling stack and all non-persistent memory in the workspace must be released, which probably involves bringing that variable back into active memory again"; Do you mean for example that a variable on the HDD that has to be suppress requires to be transferred to RAM first? Is this possible that this process is done after I stopped Matlab, while doing other things on my computer?
Excuse me in advance for this beginner question Is a routine a function I've programmed? Are the built-in Matlab functions considered as "routines"?
Thanks again,
Martin
Walter Roberson
Walter Roberson 2013년 7월 8일
"routine" includes "functions", but also includes "subroutines". Subroutines do not return values. MATLAB does not formally distinguish between the two, but computing theory does.
MATLAB that is not in debug mode would free all the indicated values first, before giving you back a prompt. However, MATLAB might not be able to (or might not be programmed to) return all of the allocated memory to the operating system, so as other tasks are given control by the operating system, the operating system might need to swap some of the MATLAB memory to disk to make room in RAM for the other programs to run.
You should consider reducing the size of your virtual memory (at the operating system level) to limit the amount of memory that can be swapped to disk. If you do that, then MATLAB would be told it had run out of memory, and it will probably then end the computation -- but you will have better system response.
Even better would be to get more memory.
Martin
Martin 2013년 7월 8일
Actually I think I was at the minimum (swap file space was equal to my RAM), and I increased it to precisely avoid receiving this error message. Anyway, this is probably a useless fiddle and I'd better improve my program/ decrease the problem size!
Thanks again!
Walter Roberson
Walter Roberson 2013년 7월 8일
It is not necessary to use any swap space at all (unless perhaps the operating system uses swap space to implement "sleep" mode.)
Martin
Martin 2013년 7월 8일
Yes, sorry, I wrote a bit quickly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by