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.