MATLAB Answers

2

Memory leak when calling DOS command in a loop

Asked by Thibaut Jacqmin on 22 Jun 2015
Latest activity Edited by Walter Roberson
on 21 Jun 2016
I have running an executable file using the "dos" command. This file is a compiled .lua file doing some calculations :
for i = 1:n
dos('executable.exe');
end
When doing so, the memory is not freed after each iteraction, and at some point it saturates. Clearing workspace, or even quiting matlab does not free the memory either. I have to restart the computer.
I checked that when running the .exe directly in a dos cmd terminal, the problem does not occur.
I am working on 2015a, windows 7 64-bits.
Anyone has an idea of what is going on ?
Thanks a lot in advance.

  0 Comments

Sign in to comment.

1 Answer

Answer by Philip Borghesani on 22 Jun 2015

Best guess is that your executable is not exiting cleanly or at all. Check task manager for a large number of some process. Most likely this has noting to do with matlab or the dos command and calling executable in a loop from a batch file would do the same thing.

  3 Comments

Thanks for your answer. Actually I checked that when running the .exe directly in a dos cmd terminal, the problem does not occur.
I also face a similar problem with the DOS command in a for-loop iteration. Here is my task:
What I want to do is to create a .txt file named " Travel_Time", by reading some other values from the source code in C++. These values exist in the .txt file "parameters.txt". Specifically this file contains only one line with two columns. I have initially put two values in the .txt myself in order to take them and afterwards, I have defined some values from the Matlab script to be read automatically. The results of the source code are combined to a simulation environment and are being written in a .txt file named "Mobil_MOVEMENT". Also, I have written a python script, which enables the console environment, which is named " python_script_2.py". My task is the following:
I want to run the Matlab environment in a for loop and each time call the DOS command, which will get the sixth column of the Mobil_MOVEMENT and will write to the Travel_Time some results. Each time , we put in the .parameters.txt new numbers for the two columns , that we have defined from the Matlab environment and based on them it creates again the new "Mobil_MOVEMENT and then it writes again to the new "Travel_Time" the new results. I want these results to be written one under the other for each line.
I attach you the code of Matlab DOS command.
function [times]=model()
%Parameters
%p=[0.1:0.1:1];
%thr=[1:0.2:2.8];
p=[0.1 0.2];
thr=[1.0 1.2];
%Create all possible parameter pairs
[A, B] = meshgrid(p, thr);
c = cat(2, A', B');
pairs = reshape(c, [], 2);
%Results vector
minimumvalue=zeros(size(pairs, 1));
maximumvalue=zeros(size(pairs, 1));
results=zeros(size(pairs, 1));
times = zeros(size(pairs, 1));
% fileID=fopen('parameters.txt','w');
% ftemp=fopen('Travel_Time.txt','w');
for i=1:size(pairs, 1)
fileID=fopen('parameters.txt','w');
ftemp=fopen('Travel_Time.txt','w');
fprintf (fileID,'%4.2f\n%4.2f\n', pairs(i, :));
% t=cputime
[status,cmdout]=dos('"C:\Program Files\TSS-Transport Simulation Systems\Aimsun 8.0.3 (R26859)\aconsole.exe" -script python_script_2.py dutch.ang');
if (status~=0)
error('console stopped')
% times(i) = 0;
else
cmdout;
report = dlmread('Mobil_MOVEMENT.txt');
times(i) = report(:, 6);
end
minimumvalue(i)=min(times(i));
maximumvalue(i)=max(times(i));
results(i)=maximumvalue(i)-minimumvalue(i);
fprintf (ftemp,'%4.2f\n', results(i));
fclose(fileID);
fclose(ftemp);
end
My problem is that the DOS command runs only for the first parameters, that are being set in the .txt as initialization and it stops there, although I have put it in a for-loop. Also, I have created the Travel_Time.txt with a column of four zeros , as those are the maximum combinations for the vectors p and thr, that you see above.
I mean I want to get each time the line one after another the new parameters in the "parameters.txt" and the new values in the "Travel_Time.txt".
Do you know why this happens?
Any hint would be greatly appreciated
Jan
on 21 Jun 2016
@Antonios Georgantas: Please do not post a question as a comment of an answer or another question. Open a new thread instead and insert a link to this thread. Thanks.

Sign in to comment.