Out of memory error when calling the same function in a for loop many times

조회 수: 18 (최근 30일)
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
Stephen23
Stephen23 2020년 10월 13일
@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.
Leerang Yang
Leerang Yang 2020년 11월 19일
Thank you for the reply @Stephen Cobeldick and sorry for not following your advice! It is a long code from my research project so I wasn't sure how I would modify it to upload. I did make sure all the file handles were closed, but the problem persisted until I added clear all at the end of the function.

댓글을 달려면 로그인하십시오.

채택된 답변

Gaurav Garg
Gaurav Garg 2020년 10월 12일
Hi Yang,
As the error suggests, you have exhausted all your memory when you try to run this function in a loop.
I would advise you to clear the memory before you run a new iteration, using clear command.
Moreover, you can take help from -
  1. https://www.mathworks.com/matlabcentral/answers/406845-out-of-memory-error
  2. https://www.mathworks.com/help/matlab/matlab_prog/resolving-out-of-memory-errors.html
  댓글 수: 2
Leerang Yang
Leerang Yang 2020년 10월 12일
Thank you for your answer. However I don't think it answers my question. Give that I am calling a function and the function does not return anything (other than writing to an output file), there is no variable in the workspace either before or after each iteration. Hence I don't see what adding the clear command will achieve.
Leerang Yang
Leerang Yang 2020년 11월 19일
The problem was in fact resolved by adding clear all to the end of the function I was calling. I am still not entirely sure why this is necessary. I thought the local variables from the function would be cleared at the end anyway without the clear all command, but it seems to have some residual effect on the memory usage!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by