How do I save PNG images using the MATLAB PCT without running into a RAM ceiling?

3 views (last 30 days)
Matlab Gurus,
I'm noticing that I run into a RAM ceiling when using parfor to save PNG files, but this is not the case when using parfor to save FIG files (I tried up to 100,000 FIG files, and RAM held mostly steady.)
The code below allows you to see what I'm talking about ...
  • To plot PNG images using a parfor loop, run the code with method = 'png' and either persist = 0 or persist = 1.
  • To plot FIG files, run the code with method = 'fig'. (No persist option applies in this case.)
Before executing the code, make sure to have first ...
  • set up a parallel pool,
  • created the PNG_SavePath and FIG_SavePath directories shown in the code, and
  • opened up the Windows Resource Monitor.
And in case you're wondering, my setup had the following specs ...
  • Windows 7, 16 cores, 16GB RAM
  • MATLAB 2015a using parpool(14)
function par_peaks
method = 'png';
numfigs = 1000;
persist = 0; % When = 1, I'm attempting to ensure that a new GUI
% window is not created if one already exists. Wasn't
% sure if this was contributing to the RAM blasting
% off or not. I'm still not sure if this works like
% I think it does in a multi-worker situation. One
% thing is for sure ... the 'persist' option makes
% no difference: RAM still blows up when plotting
% PNG files. Maybe I implemented the 'persist'
% code badly?
if strcmpi(method,'png')
PNG_SavePath = 'C:\ParPlotter\PNG_SavePath\';
if persist == 0
parfor ii = 1 : numfigs
PNG_Handle = figure('visible','off');
t = getCurrentTask();
PNG_Name = horzcat('PNG ',num2str(ii),' - TaskID ',num2str(t.ID),'.png');
[X,Y,Z] = peaks(25);
surf(X,Y,Z);
az = randi([0,359],1,1);
el = randi([-90,90],1,1);
view(az,el);
title(PNG_Name);
saveas(gcf,[PNG_SavePath,PNG_Name]);
close(PNG_Handle);
end
elseif persist == 1
parfor ii = 1 : numfigs
t = getCurrentTask();
GUI_Name = horzcat('TaskID ',num2str(t.ID));
GUI_Handle = findobj('type','figure','name',GUI_Name);
if isempty(GUI_Handle)
figure('name',GUI_Name,'visible','off');
else
clf(GUI_Handle);
end
PNG_Name = horzcat('PNG ',num2str(ii),' - TaskID ',num2str(t.ID),'.png');
[X,Y,Z] = peaks(25);
surf(X,Y,Z);
az = randi([0,359],1,1);
el = randi([-90,90],1,1);
view(az,el);
title(PNG_Name);
saveas(gcf,[PNG_SavePath,PNG_Name]);
% close(PNG_Handle);
end
end
elseif strcmpi(method,'fig')
FIG_SavePath = 'C:\ParPlotter\FIG_SavePath\';
parfor ii = 1 : numfigs
FIG_Handle = figure('visible','off');
t = getCurrentTask();
FIG_Name = horzcat('FIG ',num2str(ii),' - TaskID ',num2str(t.ID),'.fig');
[X,Y,Z] = peaks(25);
surf(X,Y,Z);
az = randi([0,359],1,1);
el = randi([-90,90],1,1);
view(az,el);
title(FIG_Name);
savefig(FIG_Handle,[FIG_SavePath,FIG_Name],'compact');
close(FIG_Handle);
end
end
end
What is the proper way to do this? SPMD block? MDCS?

Answers (0)

Community Treasure Hunt

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

Start Hunting!