MATLAB Why does my Matlab memory usage increase during a loop?

Click For Summary
Memory usage in Matlab can increase significantly during loops, especially when using Comsol scripts and functions that may not release memory properly. The user reports that their program, which runs on 8GB of RAM, fails after about 10 hours due to memory exhaustion, with usage rising by approximately 1GB per loop iteration. Despite attempts to clear variables and save data to disk, the memory does not seem to be reclaimed effectively, leading to a buildup. This issue may stem from how Matlab handles memory allocation and file saving, which can inadvertently consume additional memory. Users experiencing similar problems are advised to explore potential memory leaks and consider alternative strategies for managing memory in intensive computations.
py_engineer
Messages
11
Reaction score
0
Hi,

I am using Comsol with Matlab. I have a loop in my M-file. And it seems that the memory usage of my program increases as the loop increases.. It is a very intensive program, so after a while, the program stops because there is no more memory. For example, I am running this program on 1 CPU with 8gb of RAM. The program should probably take 15-20 hours to run, but after ~10, it stops after reaching 8gb of memory usage!

I am not very familiar with memory usage, but I was thinking that if my program is written properly, the memory usage should not increase (too much) as the loop goes on. Is that correct to say?

I thought the best way would be to copy my program and comment on it after. Here it goes:

------

clear all

flreport off

% Parameters
% ----------
q=1.6e-19;
k=1.38e-23;

% Parameters
% ----------
inv_temp=[3.33:0.8336:12.5];
temp=sort(1000./inv_temp);
size_temp=size(temp);
sz_temp=size_temp(2);

% Region 1:
xr1=0.3; % CdTe alloy composition
wr1=1e-6; % Region width
Nr1=-5e+23; % Doping level

% Region 2:
xr2=0.2;
wr2=4e-6;
Nr2=1e+21;

% Recombination:
tsrhn=1e-6;
tsrhp=tsrhn;
FFauger=0.3;
gamm=6;

% Spectral analysis
% -----------------
lambda=[2:0.25:5 5:0.05:14 14:0.25:16]; % Wavelength in microns
size_lambda=size(lambda);
sz_lambda=size_lambda(2);
p0=1e-5; % Incident flux power W/cm^2
Ts=300;
fnumber=3;

% Here I allocate memory for these two parameters that I
% use in the loop..
data=zeros(12,8); % 'data' is the matrix where I save the results from the
% calculations
Jph=zeros(sz_lambda);

% Loop
for i=1:sz_temp

% I print some parameters just to help me check where the loop's at
temp(i)

% This is my first Comsol program
flclear fem % Here I clear any previous FEM structures
% 'fem0' is an FEM structure saved in order to use as an initial solution
% for the next Comsol functions I call
[R0A,Jdark,err,fem0]=comsol_DLPH_DC(temp(i),xr1,wr1,Nr1,xr2,wr2,Nr2,...
tsrhn,tsrhp,FFauger,gamm);

data(i,1)=temp(i);
data(i,2)=R0A;
data(i,3)=Jdark;
data(i,4)=err;

for j=1:sz_lambda
lambda(j)
% This is my second Comsol program
flclear fem % Here I clear any previous FEM structures
% As you can see, 'fem0' is passed as an argument to use as an
% initial solution
Jph(j)=comsol_DLPH_spectral_bis(temp(i),xr1,wr1,Nr1,xr2,wr2,Nr2,...
tsrhn,tsrhp,FFauger,gamm,lambda(j),p0,fem0);​
end;

% This is my call of two Matlab functions I need
[Rpeak,lambda_cutoff,qeff]=spectral(lambda,Jph,Jdark,p0,temp(i));
Jbb=shotn_trapeze(lambda,qeff,1e-6*lambda_cutoff,Ts,temp(i),fnumber)

Dstar=Rpeak/sqrt(2*1.6e-19*(abs(Jdark)+Jbb)+4*k*temp(i)/R0A)

% Here I clear these functions from the Workspace
% hoping to save some memory
clear spectral
clear shotn_trapeze

data(i,5)=Rpeak;
data(i,6)=lambda_cutoff;
data(i,7)=Jbb;
data(i,8)=Dstar;

% Here I copy the results to the disk after each step because, since
% I know the program can stop before the end, I want to save what
% has been done already so that I don't have to run these cases again..
csvwrite('detectivity_wabs4.csv',data);​
end;

quit;

------

Sorry, it's very long.. But I don't think it's relevant to explain exactly what all the functions do. Basically, I have a loop calling 2 Comsol scripts (including one in another inner loop), and 2 Matlab functions at each step.

As I said, the memory builds up quite a lot after each loop step. Is that normal?? Is there something in my script that makes that to happen?? To make it simple, let's say that after each main loop step (the loop on 'temp'), the memory usage increases by 1gb..

Any comments, any help would be greatly appreciated!

Thanks a lot, have a nice week-end.
 
Last edited:
Physics news on Phys.org
Well, I know this has been posted long time ago, but I found a similar problem and reached this website through google so I guess the answer may be useful.

I think the problem with your script is that "you pretend to save data to the disk".
I guess your answer you'd be "of course", but then for some obscure reason Matlab doesn't like it.

Myself I'm going desperate because:
- I have a loop in which I solve the same femlab problem after upgrading the geometry
- I want to store the fem solution for each step on a different file.

In theory one saves things to disk to free up memory, yet in Matlab's practice it actually eats up your memory because:
- it will use additional memory to save the file
- it will never give it back no matter how hard you try

For additional reading you can google "matlab save memory leak", though I think http://www.mathworks.com/matlabcentral/newsreader/view_thread/269697" explains it best and has the simplest example to illustrate the problem.

Unfortunately I still haven't found the solution ...
 
Last edited by a moderator: