How to clear memory allocated from functions

6 次查看(过去 30 天)
Dear all,
i am running an optimization code for various operating points that contains multiple subfunctions, which generate and operate with various variables but only return those required. Is there a way to have the allocated memory of those variables cleared. Whenever I am running the code the commited Memory stacks up and the range of operating points that I can examine is restricted.
Thank you in advance.

回答(2 个)

Jan
Jan 2017-5-18
Matlab clears all locally created variables, which are not replied or persistent, automatically, if the function is left. Therefore it is only a waste of time to insert some clear commands at the end of the functions.
Do you pre-allocate all arrays properly or do they grow iteratively?
How do you check that "the commited Memory stacks up"? Note that Matlab can request memory from the OS, but it is not immediately released, when the variable is deleted. There is a memory fragmentation also, if multiple small arrays are stored distributed over the memory, such that there is no large contiguos block of free memory anymore.
So please explain the problem you observe more exactly.
  1 个评论
Dimitrios Passas
Dimitrios Passas 2017-5-18
编辑:Dimitrios Passas 2017-5-18
Regarding the committed Memory, I monitor it from the Task Manager and it slowly fills up as the code is running; and all my arrays are pre-allocated.
The code consists of a part that runs a function (containing more sub functions), that returns more or less 100MB of arrays and structures. Based on some criteria, irrelevant to the problem, I am then calculating a performance value- a number. I then alter some of the conditions each time and rerun the initial function and in case the performance value is better I store the value and the location to a structure. The structure is edited every time a better solution is found. The code runs flawlessly, computing each operating case and it only stops, in the case I have too many operating points and the committed memory reaches the ceiling which in my case is around 32GB.
My base work-space is basically empty.

请先登录,再进行评论。


KSSV
KSSV 2017-5-18
Doc clear
If you know the name of variable you an use
clear name

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by