3
$\begingroup$

Quit erases all the definitions (by quitting the kernel), but so does ClearAll["Global`*"] (or other contexts). What's the difference in terms of variables in the notebook?

$\endgroup$
  • 5
    $\begingroup$ With respect to symbols, Quit has the effect of Remove, which "removes symbols completely, so that their names are no longer recognized by the Wolfram Language." In contrast, ClearAll "clears all values, definitions, attributes, messages, and defaults associated with symbols." Following the documentation, set a = 3, run Quit, Remove, ClearAll, etc, and then execute ?a. $\endgroup$ – bbgodfrey Apr 1 '16 at 4:06
  • 3
    $\begingroup$ An experiment for you: after running ClearAll["Global`*"], try running Names["Global`*"]. Repeat the experiment, replacing ClearAll[] with Remove[]. $\endgroup$ – J. M.'s technical difficulties Apr 1 '16 at 4:08
  • $\begingroup$ One follow up question: what a name without definition and attributes after ClearAll means? Does it signify anything? $\endgroup$ – Al Guy Apr 1 '16 at 4:17
  • $\begingroup$ I am not aware of any practical distinction. $\endgroup$ – bbgodfrey Apr 1 '16 at 4:21
  • $\begingroup$ So if act with ClearAll and then execute Dumpsave what will it save in .mx file? $\endgroup$ – Al Guy Apr 1 '16 at 4:23
8
$\begingroup$

There are significant practical differences.

Quit does not "clear" anything, it instead restarts the kernel, i.e. resets it to the default state. There is a lot of internal state that changes during the session in ways that are different from creating new symbols or attaching definitions. So simply clearing (or removing) symbols won't reset the kernel. Examples include:

  • When you load a package, it does not only create its own context, it also causes $Context and $Packages to be modified

  • Symbolic results are cached (ClearSystemCache) and will actually cause symbolic processing functions to return different results than they would in a fresh session.

  • $ModuleNumber changes

  • Directory[] changes

  • pseudo-random number generator states change (and this also involves caching behind the scenes, which in principle affects memory usage)

  • Logins to various services (e.g. SocialMediaData) may be remembered until the end of the session

  • In/Out/$Line change

  • Parallel kernels keep running, ParallelNeeds keeps remembering "needed" packages, DistributeDefinitions keeps remembering what was distributed

  • A fresh kernel doesn't have all symbol definitions loaded. Using various symbols triggers loading definition and triggers loading packages.

  • etc.

These are just a few random but concrete examples that came to mind. This list is not at all meant to be exhaustive, it is simply to illustrate how many things get modified during a session. There are many more than these, very likely including many which concern the internal workings of Mathematica and we don't even know about (as they are not publicly documented).

This is all in addition to the difference between Remove and ClearAll which people mentioned in the comments and which is also significant (e.g. ClearAll won't help with shadowing but Remove will).

In short, if you are having trouble with Mathematica, reset if fully by restarting the kernel, don't just clear your own definitions.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.