Memory not being cleared from iteration to iteration?
Show older comments
Hello all,
I'm having a memory leakage somewhere in my code that I can't find. I have a matlab code that uses objects to read different files, process them and add the results to a database. In pseudocode, let's say:
for ii=1:numfiles
reader.readfile(ii);
processor.process(reader);
database.addresults(processor);
end
The "processor" object uses parfor inside. The pararllel pool is being created and deleted inside the "processor" object in each iteration. I'm running this loop over the same file several time and monitoring the ram usage (using "memory" command in windows, and monitoring the "MemAvailableAllArrays" field), and also the size of each object (using the "GetSize" function). The database is a huge matrix where I accumulate the results.
The MemAvailableAllArrays is decreasing from iteration to iteration. I thought that maybe one of the objects might be concatenating some vectors by mistake, increasing thus its size, but all objects sizes remain contant for iteration to iteration.
Any advise on how to find / solve where this memory "leak" is and/or how to solve it?
Edit: I tried with Matlab 2016b and 2020b, and same issue.
4 Comments
Raul Onrubia
on 1 Feb 2021
Stephen23
on 3 Feb 2021
"I monitored the memory usage with the MemAvailableAllArrays field of the memory command."
The MemAvailableAllArrays field gives the "Total memory available to hold data", it is not a measure of "memory usage" as you write. To measure current usage, use the MemUsedMATLAB field.
The first graph in your previous comment is confusing: did MemAvailableAllArrays contain negative values?
Just out of curiousity, how much memory does your PC have installed?
Raul Onrubia
on 4 Feb 2021
Answers (1)
Aditya Patil
on 1 Feb 2021
0 votes
As per my understanding, your code is using more memory than expected.
This might happen if you are creating many temporary variables, or if you are appending new values to a vector. You can look at the following resources for general tips on efficient memory usage in MATLAB.
8 Comments
Raul Onrubia
on 2 Feb 2021
Aditya Patil
on 3 Feb 2021
Can you provide sample code so that I can reproduce the issue? Also provide the output of the "version" command.
Raul Onrubia
on 4 Feb 2021
Aditya Patil
on 5 Feb 2021
The memory usage from the plots looks like expected behaviour to me. The memory usage seems to be constant over time, with peaks at each iteration. Note that parfor uses extra memory than for, it makes a tradeoff between memory usage and computation speed.
If you do face any memory issues in the future, you can share the code with the support team(they can work over email in case you don't want to share code publicly). Else, you can iteratively remove code and use breakpoints to detect where the memory leak happens.
Raul Onrubia
on 5 Feb 2021
Edited: Raul Onrubia
on 5 Feb 2021
Stephen23
on 5 Feb 2021
@Raul Onrubia: it seems a bit odd to me too, that MemAvailableAllArrays would decrease as your figures show.
Raul Onrubia
on 10 Feb 2021
Aditya Patil
on 11 Feb 2021
Using MemUsedMATLAB would be more appropriate than MemAvailableAllArrays. Without the code, it's difficult for me to say what causes the issue.
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





