Possible memory leak with for loop

조회 수: 9 (최근 30일)
Jeff
Jeff 2020년 7월 6일
댓글: Jeff 2020년 7월 8일
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
dpb
dpb 2020년 7월 6일
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 2020년 7월 8일
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,

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by