Clearing variables does not clear RAM

4 views (last 30 days)
Trevor Harris
Trevor Harris on 18 Apr 2016
Answered: Philip Borghesani on 25 Apr 2016
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

Answers (2)

Image Analyst
Image Analyst on 18 Apr 2016
What do you see if you put the memory command before and after the clear
memory
clear('testTemp');
memory
  3 Comments
Image Analyst
Image Analyst on 18 Apr 2016
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.

Sign in to comment.


Philip Borghesani
Philip Borghesani on 25 Apr 2016
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.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!