Possible memory leak with for loop

6 views (last 30 days)
Jeff
Jeff on 6 Jul 2020
Commented: Jeff on 8 Jul 2020
I'm running a script where MATLAB (R2019a, 9.6.0.1072779, running on Linux) uses an increasingly large amount of RAM (~1GB reserved to start), to the point where all 250BG of RAM are occupied and the machine freezes.
I've set things up such that all processing occurs within nested functions: The top level function runs a loop, and in each pass (i) a structure (~250mb) is loaded from a .mat file, (ii) the pieces of that structure are entered into a function (as individual arguments - the entire structure isn't passed), (iii) a bunch of variables are output (~17mb total), (iv) these outputs are saved a into one .mat file, and (v) everything is cleared except for three small variables (i.e., current loop index value, end value for the loop, a number used in the name of the saved .mat file). I've put a simplified version of the code below.
From everything I've read, using nested functions should release almost all the memory used within those functions when they are finished running. Also, each loop isn't keeping anything but the three small variables. Am I missing something or is this evidence of a memory leak? If the latter, is there an easy way to figure out where that leak may be occuring?
I've tried it without clearing variables at the end, without clearing JAVA, clearing all variables by name with clear (instead of clearvars -except), so none of those seem to be the culprit (or solution).
function comp_props
nmt = 5;
nms = 50;
for nm = 1:nms
load(['mats_v',num2str(nmt),'_',num2str(nm),'.mat'],'H');
[W_n,W_c,W_nc,W_co,W_nco] = comp_props_func(H.H,H.fr,H.fs,H.r,H.tn,H.tp);
save(['props_v',num2str(nmt),'_',num2str(nm)],'W_n','W_c','W_nc','W_co','W_nco');
clearvars -except nmt nms nm
clear JAVA
end
  2 Comments
dpb
dpb on 6 Jul 2020
nms isn't defined above altho that's probably not related.
Think would need to see the overall program structure including how comp_props_func is written.
Jeff
Jeff on 8 Jul 2020
Thanks! Yes, nms was defined in the actual script.
I've attached comp_props and comp_props_func. There are lots of subfunctions, which I haven't attached, but am happy to if need be,

Sign in to comment.

Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!