Why does my Matlab memory usage increase during a loop?

In summary: DLPH_DC- comsol_DLPH_spectral_bis- spectral- shotn_trapeze- comsol_trapeze- spectral- shotn- trapeze- homogeneous
  • #1
py_engineer
12
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
  • #2
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:

1. What is "Comsol Matlab memory usage"?

Comsol Matlab memory usage refers to the amount of computer memory (RAM) that is being used when running simulations or calculations using the Comsol Multiphysics software and the Matlab programming language.

2. How can I check the memory usage of my Comsol Matlab simulations?

To check the memory usage of your simulations, you can use the built-in "Memory" tool in Comsol. This tool displays the amount of memory being used by each process and allows you to monitor and optimize your memory usage. Additionally, you can also use the "Memory Profiler" tool in Matlab to analyze the memory usage of your code.

3. Why is it important to monitor and optimize Comsol Matlab memory usage?

Monitoring and optimizing memory usage is important because running simulations and calculations in Comsol and Matlab can require a large amount of memory. If the memory usage is too high, it can slow down the simulation or even cause it to crash. By monitoring and optimizing memory usage, you can prevent these issues and ensure the efficient and successful execution of your simulations.

4. How can I reduce the memory usage of my Comsol Matlab simulations?

To reduce memory usage, you can try simplifying your model or using the "Adaptive Mesh Refinement" feature in Comsol to reduce the number of elements in your simulation. Additionally, you can also optimize your code in Matlab by preallocating arrays, minimizing unnecessary variables, and using efficient algorithms.

5. What should I do if my simulations are still using too much memory?

If your simulations are still using too much memory, you can try running them on a computer with more RAM or consider using a parallel computing option in Comsol or Matlab to distribute the memory load across multiple processors. You can also reach out to Comsol or Matlab support for further assistance in optimizing your memory usage.

Back
Top