Clearing variables does not clear RAM

조회 수: 3 (최근 30일)
Trevor Harris
Trevor Harris 2016년 4월 18일
답변: Philip Borghesani 2016년 4월 25일
Hey all,
So I'm working with some large file and I'm extracting data from said files using the code below:
for iTest = 1:length(testFiles)
disp(['Loading Test ', num2str(iTest), ' of ', num2str(length(testFiles))])
testTemp = load([pwd, '\RESULTS\', testFiles{iTest}, '\', testFiles{iTest}, '.mat']);
segments = size(testTemp.dataStruc(chan2idx(testTemp.dataStruc,'PITCH')).data.segments,1);
for iSeg = 1:length(segments)
pitchMean(end+1) = testTemp.dataStruc(chan2idx(testTemp.dataStruc,'PITCH')).data.(['S', num2str(iSeg)]).mean.data;
fuelMean(end+1) = testTemp.dataStruc(chan2idx(testTemp.dataStruc,'FUEL_COMP')).data.(['S', num2str(iSeg)]).mean.data;
speedMean(end+1) = testTemp.dataStruc(chan2idx(testTemp.dataStruc,'SPEED')).data.(['S', num2str(iSeg)]).mean.data;
end
clear testTemp
end
All well and good. The only issue is that the clear testTemp at the end of every loop does not seem to clear the memory. My ram keeps climbing! The removal of testTemp is verified by the whos command. See the attached screenshot which illustrates this odd behavior. Any ideas as to what may be going on?
It may also be worth noting that this behavior happens to me all the time, not just in loops. I'm only now posting out of frustration.
Thanks! Trevor

답변 (2개)

Image Analyst
Image Analyst 2016년 4월 18일
What do you see if you put the memory command before and after the clear
memory
clear('testTemp');
memory
  댓글 수: 3
Image Analyst
Image Analyst 2016년 4월 18일
Trevor, you can't do it from the command line - the variable has already been cleared by that point.
Put those lines of code in your script, replacing the one clear you have now, just before the end of your for loop. Then show us what gets displayed in the command window.
Trevor Harris
Trevor Harris 2016년 4월 25일
My apologies. See attached.

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


Philip Borghesani
Philip Borghesani 2016년 4월 25일
Some questions and thoughts:
  1. Task manager is not a very good way to track memory use by an application. I suggest looking at something like process explorer and just looking at the private bytes graph for the MATLLAB task using process explorer from sysinternals (Microsoft).
  2. What is in these mat files? Are there any globals? please show us the output of whos -file for one (the largest?) of these files.
Your code is growing 3 arrays in each loop, doing so is fragmenting memory, that along with loading large mat files could cause the symptoms you are seeing due to memory fragmentation.

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by