Out of memory error

12 views (last 30 days)
Luccas S.
Luccas S. on 18 Jul 2024
Commented: Walter Roberson on 30 Jul 2024
I'm trying to run the code below, it works well and does its job. The problem is that it is not capable of running all iterations continuously, the out of memory error always appears and I end up having to restart it or start it running from where it left off. I've already made some changes, but the error still persists. Does anyone have any suggestions or ideas on how I can get around it?
The algorithm saves one image from each cycle, with each iteration almost 1800 images are generated, I don't know if this is what is causing the error to occur. But using the close(figh) command it closes the image that is used to save...
clear all; close all; clc; warning off;
baseDir = 'C:\Users\Lucas\Documents\Mestrado\wsst';
caso = 2; % Selecionando o caso 2 para simulação
N = 32;
ciclo = 128;
tipos_de_falta = ["A", "B", "C", "AB", "BC", "CA", "ABC"];
hs = [0, 25, 50, 75, 100];
runs = 1:9;
for idx_tipo = 1:length(tipos_de_falta)
for idx_h = 1:length(hs)
for idx_run = 1:length(runs)
clearvars -except baseDir caso N ciclo hs tipos_de_falta runs idx_tipo idx_h idx_run
tipo_falta = tipos_de_falta(idx_tipo);
h = hs(idx_h);
run = runs(idx_run);
if caso == 2
[Ip_faseA, Ip_faseB, Ip_faseC, Is_faseA, Is_faseB, Is_faseC, If_faseA, If_faseB, If_faseC, t, instanteChaveamento, prefixo_arquivo, tipo_falta_descricao] = PSCAD_2(h, N, tipo_falta, run);
else
disp('Opção inválida. Por favor, escolha 1, 2 ou 3.');
break;
end
pastaCaso = ['Falta_', prefixo_arquivo, '_em_', num2str(h),'_da_LT'];
casoPath = fullfile(baseDir, pastaCaso);
if ~exist(casoPath, 'dir')
mkdir(casoPath);
end
chaveamentoFolderName = sprintf('Chaveamento_%0.6f_s', instanteChaveamento);
chaveamentoFolderName = regexprep(chaveamentoFolderName, '[\s\.]', '');
chaveamentoPath = fullfile(casoPath, chaveamentoFolderName);
if ~exist(chaveamentoPath, 'dir')
mkdir(chaveamentoPath);
end
subFolders = {'pre_falta', 'chaveamento', 'falta'};
phases = {'Fase A', 'Fase B', 'Fase C'};
for i = 1:length(subFolders)
subFolderPath = fullfile(chaveamentoPath, subFolders{i});
if ~exist(subFolderPath, 'dir')
mkdir(subFolderPath);
end
for j = 1:length(phases)
phaseFolderPath = fullfile(subFolderPath, phases{j});
if ~exist(phaseFolderPath, 'dir')
mkdir(phaseFolderPath);
end
end
end
Ida = If_faseA;
Idb = If_faseB;
Idc = If_faseC;
Ida = normalize(Ida);
Idb = normalize(Idb);
Idc = normalize(Idc);
total_a = length(t);
total_d = t(end) - t(1);
fs = total_a / total_d;
for i = 1:(total_a - ciclo + 1)
i_limite = min(i + ciclo, total_a);
tmin = t(i);
tmax = t(i_limite);
if tmin >= 1
break;
end
if tmax < instanteChaveamento
pasta_destino = 'pre_falta';
elseif tmin >= instanteChaveamento
pasta_destino = 'falta';
else
pasta_destino = 'chaveamento';
end
for fase = ['a', 'b', 'c']
[sst, f] = wsst(eval(['Id' fase '(i:i_limite)']), fs, 'ExtendSignal', true);
figh = figure('Visible', 'off');
pcolor(t(i:i_limite), f, abs(sst));
shading interp;
set(figh, 'Position', get(0, 'Screensize'));
set(gca, 'Visible', 'off');
colorbar('off');
caminho_do_arquivo = sprintf('%s\\%s\\Fase %s\\TF_Ridges_Fase_%s_%s_em_%d_LT_%d.jpg', ...
chaveamentoPath, pasta_destino, upper(fase), upper(fase), prefixo_arquivo, h, i);
exportgraphics(gca, caminho_do_arquivo);
close(figh); % Fechar a figura atual para economizar memória
end
end
end
end
end
  2 Comments
Walter Roberson
Walter Roberson on 18 Jul 2024
I do not see any obvious reason.
I am a bit concerned about the use of eval()
Walter Roberson
Walter Roberson on 30 Jul 2024
It would be more robust to be explicit which axes you are working with
figh = figure('Visible', 'off');
ax = axes('parent', figh);
pcolor(ax, t(i:i_limite), f, abs(sst));
shading(ax, 'interp');
set(figh, 'Position', get(0, 'Screensize'));
set(ax, 'Visible', 'off');
colorbar(ax, 'off');
caminho_do_arquivo = sprintf('%s\\%s\\Fase %s\\TF_Ridges_Fase_%s_%s_em_%d_LT_%d.jpg', ...
chaveamentoPath, pasta_destino, upper(fase), upper(fase), prefixo_arquivo, h, i);
exportgraphics(ax, caminho_do_arquivo);
close(figh); % Fechar a figura atual para economizar memória

Sign in to comment.

Answers (1)

Ashutosh Thakur
Ashutosh Thakur on 23 Jul 2024
Hello Luccas,
The approach of splitting the codebase into the calculation part and the graphics part can be done, and then both sections can be profiled for benchmarking. You can use the memory function in MATLAB to identify memory usage and calculate the memory usage of different operations, thereby identifying the operations that are causing memory issues. You can refer to the following documentation links:
To resolve Out of Memory issues, you can follow the following link on the strategies and methods described in the following link:
I hope this helps you!
  1 Comment
Luccas S.
Luccas S. on 29 Jul 2024
The error that appears is this:
Error using exportgraphics
Out of memory
exportgraphics(gca, caminho_do_arquivo)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!