Memory leak or problem in my code ?

2 views (last 30 days)
Eskapp
Eskapp on 11 Jul 2012
Hello everybody,
I wrote a small Matlab function (code just below) with really common Matlab functions. The main aim of this function is to calculate a big cell array : ~ 12000 * 8 cells and to put in each cell a vector of ~ 12000 values. At the end of this function, a 3D graph is plotted through mesh function. There is no output argument.
However, once the function has run and the graph is closed, the memory used keep being unavailable. I tried 'pack' function and, of course, 'clear all'. The memory keeps being unavailable for this and for other Matlab sessions (I tried to open another independent Matlab and I was able to saturate the memory of the server I'm working on.) I also checked there was no persistent or global variables.
The only way I have to release the memory is to exit Matlab.
Is there anything to do to my code to avoid this memory problem ? I work with Matlab 7.12.0 (R2011a) on Unix
Last precision : I tried to run my function : - without plotting the graph - not using the conv and trapz functions inside - clearing variables inside the function when it was possible, the problem remained the same...
Thank you for your help !
Here is the code :
function [] = dynamique_observation()
clear all; close all; clc;
nb_wksp = 12060; % Workspaces = iterations
%%Parameters calculation
pas_min = 900*19.9e-3/3600; % step
alph0_ref_min = 4.8518*60; % reference angle
alph0_ref_rad = alph0_ref_min/60*pi/180; % rad conversion
alph0_min = ...
(alph0_ref_min-6029*pas_min:pas_min:alph0_ref_min+6030*pas_min); % Angles vector
alph0_rad = alph0_min/60*(pi/180); % rad conversion
c = 299792458; % light speed in vacuum
R = 6378100; % Earth radius (m)
lat = 47.21; % latitude (deg)
Vt = R*cosd(lat)*2*pi/86400; % Speed (m.s-1)
% Frequencies vector
f_centrale = [1380e6+880+2200 1385e6 1390e6 1395e6 1.4e9 1405e6 1410e6 ...
1415e6 1420e6-880-2200];
lambda = zeros(1,9); % Initialization
for bf=1:9 % spatial frequencies calculation
lambda(bf) = c/f_centrale(bf);
end
D = zeros(1,8); % Initialization
for dec=0:7 % Distances calculation
D(dec+1) = dec*2.5e-3*Vt;
end
%%Moon model
chp_lune = zeros(1,nb_wksp); % Initialization
chp_lune(1:3015) = 3; % Sky
chp_lune(3016:9044-floor(6029*0.14)) = 190; % Non illuminated part of the Moon
chp_lune(9044-floor(6029*0.14)+1:9044) = 300; % Illuminated part of the Moon
chp_lune(9045:nb_wksp) = 3; % Sky
% Convolution with a gaussian function
sigma = (2/60)*pi/180; % sigma=2'
alph_minProfil =...
(alph0_ref_min-nb_wksp/2*pas_min:pas_min:alph0_ref_min+(nb_wksp/2-1)*pas_min); % angle vector
alph_radProfil = alph_minProfil/60*pi/180; % rad conversion
gauss_refProfil = 1/(sqrt(2*pi)*sigma)*...
exp(-(alph_radProfil-alph0_ref_rad*ones(1,nb_wksp)).*(alph_radProfil-alph0_ref_rad*ones(1,nb_wksp))/(2*sigma^2)); % Gaussian definition
profil_lune = conv(chp_lune,gauss_refProfil,'same'); % Convolution
maxi = max(profil_lune); % max search
normalisationfactor = 300/maxi; % Normalization factor
profil_lune2 = profil_lune*normalisationfactor; % Normalization
%%Creation of the interference function
sinus_interf = cell(nb_wksp,8); % Initialization
interf_moy_temp = zeros(1,nb_wksp); % temporary, for calculation in the loop
interf_conv = cell(nb_wksp,8); % Initialization
tic
for wksp = 1:nb_wksp % Loop on workspaces
disp(num2str(wksp))
alph_min = ...
(alph0_min(wksp)-nb_wksp/2*pas_min:pas_min:alph0_min(wksp)+(nb_wksp/2-1)*pas_min); %angle vector
alph_rad = alph_min/60*pi/180; %rad conversion
for dec = 0:7 % Loop on dec
for bf = 1:9 % Loop on frequencies
interf = 2*profil_lune2(wksp)^2*(1+cos(2*pi*alph_rad*D(dec+1)/lambda(bf))); % Interferences calculation for alph0
interf_moy_temp = interf_moy_temp + interf; % Mean calculation
end % And of loop on frequencies
sinus_interf{wksp,dec+1} = interf_moy_temp/9; % End of mean calculation
interf_moy_temp = 0; % Initialization for next loop
end % End of dec loop
%%Gaussian definition
gauss = 1/(sigma*sqrt(2*pi))*...
exp(-(alph_rad-alph0_rad(wksp)*ones(1,nb_wksp)).*(alph_rad-alph0_rad(wksp)*ones(1,nb_wksp))/(2*sigma^2)); % Gaussian def
%%Final signal
for dec2 = 0:7 % Loop on dec
interf_conv{wksp,dec2+1} = conv(sinus_interf{wksp,dec2+1},gauss,'same'); % Convolution
end
end % End of the workspaces loop
toc
%%Integration step
mat_res = zeros(nb_wksp,8); % Initialization
for k = 1:nb_wksp % Loop on workspaces
for l=0:7 % Loop on dec
mat_res(k,l+1) = pas_min/60*pi/180*trapz(interf_conv{k,l+1}); % Integration with trapz method
%interf_conv{k,l+1}=[];
end % End of loop on dec
end % End of loop on workspaces
%%cleanup
%clear interf_conv
%%Graph
mesh(0:7,1:nb_wksp,mat_res(:,1:8))
colorbar
alpha(0.1)
xlabel('Dec')
ylabel('Workspaces')
zlabel('Amplitude')
title('Integral of interference diagram with antenna diagram')
  3 Comments
Kye Taylor
Kye Taylor on 11 Jul 2012
Perhaps it is this loading into memory that @Jan refers to that accounts for the memory that is not being released after execution of the function?
Eskapp
Eskapp on 12 Jul 2012
Thanks for comments.
When I say that "memory used keep being unavailable" I mean : Once my function has completely run, I can see on the 'top' screen of my Unix Konsole that I am using something like 37% of the resident memory of the server I am working on. If I open another Matlab session, totally independent from the first one and if I create big vectors for instance which would take 70% of the memory of the server, the server will be out of memory.
Second example : If I keep working on the first session and if I run again my function, after the second run, the memory used will be 74% and so on until the server is out of memory.
Concerning mex-function : I am not using this kind of functions and, more generally, there is no user-define function and no file or data loading in this code.
It is a really basic code and I was very surprised to see such a memory problem when running it. That's why I came to ask for help here.

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!