Non-Deterministic Memory Allocation?

MATLAB’s clear/clearvars is made feasible by MATLAB’s atypical workspace system of storing variables. This links to a somewhat old answer, but it’s a more detailed description of workspaces and their unique caveats.

This just doesn’t translate to languages with modules holding their own global variables, so this feature will never be fully replicated, if at all. Let’s take a simple case where you fire up the REPL and work in Main. After polluting the namespace with a bunch of variables and data, you decide to remove them. First problem: you want to keep the already compiled functions; okay, we’ll keep any variable assigned to an instance of Function. Second problem, you don’t want to remove const variables because they were inlined into your compiled functions and can’t be safely reassigned anymore; fine, let’s keep them too. Third problem, the functions relying on non-const global variables now don’t work, and you have to manually redefine them from memory. And now you realize that MATLAB risks easier naming conflicts by throwing global variables into their own workspace so that clear ... doesn’t easily cause such issues (though clear global ... and clear all do remove things from the global workspace and is discouraged even in clear’s documentation).

In most languages, the repeatable unit of code is a function, and that should be used instead of tweaking and rerunning scripts. This practice is recommended in MATLAB as well by the real junkies.

6 Likes