How can I clear variables in a parallel for loop efficiently?

60 views (last 30 days)
In order to maintain low memory usage in my code, I need to delete variables used inside of a parallel process. One could manually assign each variable "[ ]", but I am looking for a simpler method that can deal with variables in bulk. Using "parfor" on nested loops allows the use of "clearvars", but significantly increases computation time. Any help would be appreciated.
  2 Comments
David Hill
David Hill on 29 Nov 2022

If the sizes will stay the same, why not just write over them?

Jordan Peper
Jordan Peper on 29 Nov 2022
The variables are not pre-allocations. They exist locally in the loop to append to a struct and other temporary uses. I want to remove them from the loop to maintain lower memory usage as the code that follows executes.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 29 Nov 2022
Edited: Walter Roberson on 29 Nov 2022
I investigated clearvars a couple of years ago. It turned out to be rather slow. There was a regexp step involved even when specific variable names were given.
If you have a list of specific variables (no wild-carding) then it is (or at least was) much faster to clear them directly than to call clearvars to clear them.
But it sounds as if you want to be able to use the wildcarding facility, in order to avoid having to name each variable in buik. In that case, Yes, it is going to be slow.
Note that clearing variables generally slows down the execution engine. The next time the name is used, the execution engine needs to do name resolution on the name all over again, including potentially having to recheck whether the name is a function or not -- and the execution engine seems to have some logic that tracks variable classes (because if a variable changes class, then you might need to redo name resolution of functions operating on the variable.)
Because of these kinds of factors, if you need to reclaim memory, it is more efficient to leave the name intact, but to assign to it the empty array of the same type as it already is, perhaps something like
G = zeros(0,0,'like',G)
if you do not want to track the type yourself.. but even faster is to use empty if you are okay with knowing the type, such as
G = uint16.empty;
This will not help you do bulk clearing of variables though.
Perhaps you should be calling functions, which would automatically remove memory for local variables upon return?

More Answers (0)

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!