Clearing variables does not clear RAM

3 次查看(过去 30 天)
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.

请先登录,再进行评论。


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