Memory leak; matlab seems to not be clearing temporary variables?
81 次查看(过去 30 天)
显示 更早的评论
Hey guys,
Just wondering if this had been addressed yet. There's lots of posts on memory leak but most of them seem pretty specific to a particular task but this is pretty general so I thought I'd post it in case others have the same general problem. Also was wondering if the issue had been fixed.
I have linux 64-bit matlab r2012a. The simple function below has problems as it sometimes accumulates memory and doesn't seem to free it after the function is done. The "sometimes" is kind of strange too, but for me if I open up matlab, open up the function below, and run it cold turkey memory will be freed up afterwards. If, however, I change n from 100 to 400, then it will start to accumulate memory (e.g. it goes from 450M 4.0ishG before settling down to 1.9G after function exit). This is strange behavior and doesn't seem right. It is akin to the behavior noted at the following location
and it is possible this corresponds to matlab in some circumstances not clearing temporary variables that it creates when only parts of workplace variables are handed in to a function. From what I've seen Matlab likes to blame the operating system for not freeing up the memory but I'm not sure I completely buy that so explanations other than "it's the operating system's fault" would be appreciated.
So, has this been addressed yet? A quick fix is to hand in full variables instead of parts of variables...but I was just wondering about the status and thought it might be useful for others if I post this here as a caveat that it might happen.
function mem_test
n = 100;
for j = 1:n
tmp_cell{j} = rand(1000,1000);
end
for j = 1:n
mys(j) = my_sum(tmp_cell{j});
end
mys
end
function val = my_sum(A)
val = sum(A(:));
end
2 个评论
Adam
2015-3-12
Do you mean the memory remains after you exit the outer function rather than the subfunction?
Also what does
mys
do?
回答(2 个)
Zeng
2020-1-16
Seems still not sovled, I searched a lot posts but didn't see any answers to this. One thing for sure is that clear doesn't work in Linux.
5 个评论
Image Analyst
2021-10-13
I ran it on Windows 10 and it went from 1.7 GB to 4.8GB and stayed there no matter how many times I ran it afterwards. If you put "clear all" as the first line of the code, does that make it be 4.6 GB every time or does it continue to climb?
Thomas
2023-9-28
编辑:Walter Roberson
2023-9-28
I think this issue is limited to linux but is still present in 2021b. It seems to be limited to cell arrays, structs which uses non continous memory blocks. In contrast e.g. double arrays are removed from memory by using clear.
I have a long term application and the "flood level" of memory is increasing more and more over time. clear or delete of these variables has no effect. (I've measured the ram with vmstat)
The support mentioned mallopt options ( https://man7.org/linux/man-pages/man3/mallopt.3.html ) but i have no idea how to use them.
Is there still no solution how to free not used cell arrays from memory?
Jan
2015-3-12
How did you measure the memory consumption? Why do you assume that occupying temporary memory is a fault?
Letting an array grow iteratively is a very bad programming pattern. This is a problem in any programming language and not specific to Matlab. Then the OS has to "decide", if new memory is allocated or formerly allocated memory is cleared and reused. Both strategies have advantages and disadvantages.
It would be "fault", if the OS or Matlab does not release the temporarily used memory, when it is requested by another program. Is this the case? If a programmer thinks, that the temporary occupation of memory is a fault or not efficient for any reasons, it is the best way to avoid the allocation large temporary blocks of memory with a high frequency.
2 个评论
per isakson
2015-3-12
编辑:per isakson
2015-3-12
@Jan, have you reproduced the behavior, which Jared describes? I have failed to reproduce it on R2013b,64bit,Windows7. I have tested with both the example code in the question and code from the blog-post, which Jared refers to.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!