I have some C++ code that communicates with Matlab via the Engine C API. My code creates temporary variables in the Matlab workspace, which it diligently cleans up via clear
calls as soon as possible. However, at some point, my application fails, telling me that it is unable to create the next Matlab temporary variable (usually after ~65530 such operations).
After some experimentation on the Matlab command line, I discovered I could recreate this problem in pure Matlab (that is, independent of my C++ code and its use of the Engine API). Consider the following code:
for i = 1 : 100000
eval(sprintf('x_%d = %d', i, i));
whos
eval(sprintf('clear x_%d', i));
whos
end
Executing this code on my 32-bit windows laptop with Matlab R2008B (ancient, I know), the loop eventually aborts with the error message:
The current workspace already has too many variables; there is no room for "x_65532".
So, it seems that at least this obsolete version of Matlab has a 64K symbol table limit. Perhaps the limit is bigger on newer (64-bit) versions of Matlab--I'd be interested to hear what results others get.
However, the more interesting question is what effect the clear
call is having and how to work around its odd behavior. Here is the output from an iteration a little prior to the abort:
x_65530 =
65530
Name Size Bytes Class Attributes
i 1x1 8 double
x_65530 1x1 8 double
Name Size Bytes Class Attributes
i 1x1 8 double
As you can see, the whos
output clearly shows that the temporaries from the prior iterations have been removed from the workspace, and the clear seemingly works as expected. Nonetheless, the symbol table has apparently reached capacity.
So, two questions for the SO faithful:
- How can I work around this somewhat arbitrary limit? That is, what should my
clear
calls be replaced with? - Has this behavior changed with newer and/or 64-bit versions of Matlab?
Repeating my comment in form of an answer:
If you have to stick to the variable naming, you could try to re-use variable names, once they were cleared, avoiding the creation of 65xxx different variable names.