Matlab memory use increases every time I run my script

Whenever I run my script MATLAB.exe increases in size by several hundred Mbytes, as reported by the Windows resource monitor. After a few trials my memory is filled.
How do I fix this? Currently I remedy this by closing and restarting Matlab.
I am running Matlab using a 64 bit version of Windows 7 with 8GB of RAM.

답변 (1개)

Jan
Jan 2016년 11월 17일

1 개 추천

Without seeing any details, it is impossible to guess, what's going on. There is no general "do not let Matlab waste memory" flag. Perhaps your script stores more and more values in a growing array? If you in addition forgot to pre-allocate it (search for the term in this forum in case of doubts), the memory consumption is a problem of your script and not of Matlab. A whos and a feature memstats should reveal some details.
Do you use any C-Mex scripts provided by users?

댓글 수: 3

Thanks for looking into this. I am relatively new to Matlab and having trouble reconciling what Matlab has to say about memory management.
In fact I do not pre-allocate in many cases. I am gathering captured data from a file and categorizing. I don't know whether to pre-allocate 0 or 100000 spots, or in which category.
I know I should and perhaps I can find a way to do this. Matlab documentation recommends pre-allocation often, but I haven't seen the discussion on how it affects memory management.
However, what I read of memory management is that Matlab deallocates memory by itself and there is nothing for me to do in regards to memory management. I exit my script in a controlled manner so I assume that the memory will be cleared.
Is it the case that ignoring pre-allocation makes the memory unrecoverable?
To fix this am I better off gathering data within a function to an object of an unknown size, then transferring to a dynamically pre-allocated object on the output parameter list as I exit the function?
Are there any other practices that interfere with memory management?
Show us a SMALL segment of code with which you can reproduce the problem and a SMALL sample of data needed to run that code and we may be able to offer much more specific suggestions.
Jan
Jan 2016년 11월 21일
Matlab does release memory automatically, when it is not used anymore. And the OS will clean this memory automatically as soon as it finds the time to do this. A proper pre-allocation (better reserve too much memory then letting an array grow) reduces the need to cleanup memory and is important for the performance.
The memory might be occupied by persistent variables or by a huge number of objects in a figure. So please show us some code, which reproduces the problem. Otherwise an explicit suggestion would be based on guessing.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2016년 11월 17일

댓글:

Jan
2016년 11월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by