Out of memory error when calling the same function in a for loop many times
18 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Leerang Yang
el 7 de Oct. de 2020
Comentada: Leerang Yang
el 19 de Nov. de 2020
I have a function that performs a stochastic simulation and writes results into a csv file.
I have a script:
function RunMultipleSimulations(parameters)
for i=1:N
RunSingleSimulations(parameters, output_filename)
end
end
When I do this for a large number of N, I get out of memory error. I don't understand why this is the case because I thought all variables from calling the function would get cleared at the end and free up the memory.
My workaround has been to run RunSingleSimulations(parameters, ~) individually and then combining the output file at the end. I never get out of memory error while running the function a single time. So I suppose there's some buildup of memory usage by calling it in a for loop, and would like some help understanding why and how to deal with it.
2 comentarios
Stephen23
el 13 de Oct. de 2020
@Leerang Yang: please upload the function RunSingleSimulations (or an MWE that demonstrates the issue) by clicking the paperclip button. What is the value of N? Are any file handles correctly closed at the end of the function?
Without seeing the function we have to resort to guessing, which is not an efficient way to debug.
Respuesta aceptada
Gaurav Garg
el 12 de Oct. de 2020
Hi Yang,
As the error suggests, you have exhausted all your memory when you try to run this function in a loop.
Moreover, you can take help from -
- https://www.mathworks.com/matlabcentral/answers/406845-out-of-memory-error
- https://www.mathworks.com/help/matlab/matlab_prog/resolving-out-of-memory-errors.html
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!