Out of memory error

37 次查看(过去 30 天)
Luccas S.
Luccas S. 2024-7-18,15:32
回答: Ashutosh Thakur 2024-7-23,6:48
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
  1 个评论
Walter Roberson
Walter Roberson 2024-7-18,17:47
I do not see any obvious reason.
I am a bit concerned about the use of eval()

请先登录,再进行评论。

回答(1 个)

Ashutosh Thakur
Ashutosh Thakur 2024-7-23,6:48
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!

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by