A MATLAB program is experiencing severe memory issues when using parallel processing (parfor). The program generates a series of ECell objects, storing them in a 20×20 object matrix. Despite the total size of this matrix being only about 120KB, the program’s memory usage rapidly increases to over 20GB when using parfor, eventually causing MATLAB to crash. This excessive memory usage occurs as soon as one of the parallel iterations completes and attempts to store its result in the main matrix.
Original Code:
Main script
clear
Num_series = 20;
Num_gen = 20;
Cells_series(Num_series, Num_gen) = ECell();
inicell = ECell();
cellpara = CellPara();
parfor kkk = 1:Num_series
Cells_series(kkk, :) = series_generater(inicell, cellpara, Num_gen);
end
function
function [Cellvec] = series_generater(inicell, cellpara, Num_gen)
[~, Newcell2, inicell.CellsWhenIni, inicell.ini_t, inicell.ini_tag, ~, ~] = RunTillDiv(inicell, cellpara, 100);
Cellvec(1, Num_gen) = inicell;
for kk = 1:Num_gen
Newcell2 = Newcell2.RenormCell;
[Newcell2, ~, Newcell2.CellsWhenIni, Newcell2.ini_t, Newcell2.ini_tag, ~, ~] = RunTillDiv(Newcell2, cellpara, 10000);
Cellvec(kk) = Newcell2;
end
end
Key Points:
The problem occurs only when using parfor for parallel execution.
The total size of the data structure (Cells_series) is approximately 120KB.
Memory usage inexplicably increases to over 20GB.
The issue arises when a single parallel iteration completes and attempts to store its result.
This behavior suggests a possible bug in MATLAB’s parallel computing implementation rather than a simple memory management issue in the user code.
Most of methods related to optimizing memory usage have been used, and the problem doesn’t seems related to that, since the biggest data: Cells_series is actully 120kb.
KQing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1