Parsim function consumes lot of memory. How to clear temporary MATLAB files?

23 次查看(过去 30 天)
Hello everyone,
I'm running lots of simulations with parsim function. I realized that MATLAB started to store lots of temporary files in the "AppData\Local\Temp" folder. The used memory was increasing rapidly, and I received an alert from windows that the system memory was running out!
Then I divided the simulation in several parsim sessions, but the memory runned out again. Does it exists a way to clear those temporary files in that folder which have been created by MATLAB? Maybe I could do it between the various parsim sessions.
Thank a lot, Antonio

回答(3 个)

Max Helbing
Max Helbing 2019-2-20
编辑:Max Helbing 2019-2-20
I'm running into the same problem. When I use
parsim(simIn)
MATLAB stores loads of data in "AppData\Local\Temp".
I already tried
Simulink.sdi.cleanupWorkerResources
after all simulations are done. But this is a just kind of a "cleanUpFcn" and does not prevent MATLAB to increasingly consume memory while performing parallel simulations.
I don't know where to integrate:
parfevalOnAll(gcp, @sdi.Repository.clearRepositoryFile, 0)
Is there a way to implement a "cleanUpFcn" that deletes all the unused temporary files in the simIn.postSimFcn? This would clean temporary working folders after each simulation.
  3 个评论
Max Helbing
Max Helbing 2019-9-26
Unfortunately, I haven't found a solution. To make it work, I cleared memory, which is probably the simplest but definitely not the most elegant solution.
Antonio Carlo Bertolino
I inserted at the end of the script the command line
parfevalOnAll(gcp, @sdi.Repository.clearRepositoryFile, 0)
suggested by @Tony Mohan Varghese.
In addition, since my script creates several files that I don't want to save, before launching parsim, at the beginning of the script, I've added the following lines:
% Add to path the current folder (will be removed at the end of the script)
cwd = pwd;
oldpath = path;
path(oldpath,pwd)
tmpdir = tempname;
mkdir(tmpdir);
cd(tmpdir);
which add the current working directory to the path and change the working directory to a temporary one.
Then, all the code is executed.
In the end, the following lines try to remove ALL the temporary folders created by MATLAB in the current and in the past sessions, if any.
cd .. % Back up by 1 level in the folder tree
% Remove the temporary folder
list=dir;
result=regexp({list.name},'tp','match'); % Temporary folders start with tp
ind_folder=find(~cellfun(@isempty,result));
for kk = 1:length(ind_folder)
folder = list(ind_folder(kk)).name;
if 7 == exist(list(ind_folder(kk)).name,'dir')
try
rmdir(folder)
disp(['Removed folder: ',list(ind_folder(kk)).name])
catch
disp('Ehi, folder',list(ind_folder(kk)).name],' not removed!')
end
end
end
clear folder list result ind_folder
cd(cwd) % Come back to the original working directory
path(oldpath) % Restore the original path list, removing the original working directory
This helped me to delete most of the temporary folders every time the script is launched.
The temporary folders created by MATLAB starts with "tp".
Hope this can help.

请先登录,再进行评论。


Tony Mohan Varghese
Use the following command to clear the temporary files.
>> parfevalOnAll(gcp, @sdi.Repository.clearRepositoryFile, 0)
Temporary files are usually .dmr files created by SDI (Simulink Data Inspector). Here are the references: https://in.mathworks.com/help/distcomp/parfevalonall.html https://in.mathworks.com/help/simulink/slref/simulink.sdi.cleanupworkerresources.html

Reid Spence
Reid Spence 2024-4-12

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by