Here are a few very simple but extremely useful tricks for heavy Mathematica users. They're all quite trivial, but since they address very common and annoying problems that seem to concern many people, I thought I'd make them available to google searches :).

 

Memory Management

Mathematica is a terrible memory hog. When analyzing large data sets and reading/writing them to disk, the Kernel can grow in memory to > 1 Gb, with an eventual crash all but assured.

Here's a simple command you can run every now and then to ensure that Mathematica will never crash due to Kernel Bloat:

ClearMemory := Module[{},
         Unprotect[In, Out];
         Clear[In, Out];
         Protect[In, Out];
         ClearSystemCache[];
         ];

Just insert this command into any especially 'strenuous' routines, and your Kernel will live forever.

What this command actually does is clean up the memory used my Mathematica, though it will not decrease the amount of memory allocated to Mathematica by the operating system. However, running this command regularly will ensure that your Kernel never grows beyond a few 100 Mb.

 

Loading/Saving any Mathematica Objects in their original form

This is completely trivial, but extremely useful.

(The following functions perform almost the same tasks as the native Mathematica 'Save' (and 'Get') commands, but the usage is slightly different in that you specify the content of a variable to save, rather than the definition of that specific variable, which I personally like better.)

Any mathematica expression (be it a plot, a variable, a table/matrix,... ) with any content can be saved to disk in its original form by converting it to a string using InputForm. To load it again use ToExpression to recover the original object. This is implemented with these very simple commands:

SaveIt[filename_, expr_] := Module[{output},
           output = 
           Export[filename <> ".dat", ToString[expr // InputForm], 
           "String"];
           ClearMemory;
           output
           ];
SaveIt[varnamestring_] := Module[{output},
           output = 
           Export[varnamestring <> ".dat", 
           ToString[ToExpression[varnamestring] // InputForm], "String"];
           ClearMemory;
           output
           ];
ReadIt[filename_] := Module[{output},
           output = 
           ToExpression[
           Import[StringReplace[filename, ".dat" -> ""] <> ".dat", 
           "String"]];
           ClearMemory;
           output
           ];

Note that SetDirectory should be used to specify output directory.

Usage:
To save a variable as its original name, run
SaveIt["variablename"].
To change the name under which it is saved, use
SaveIt["saveasfilename", variable].
To recover the variable, run
variablename = ReadIt["savedfilename"].

Having these commands defined is especially nice because it's so simple to save something. If you run an evaluation that takes a little while (even just a few minutes), just run
SaveIt["variablename"]
on the output variable, and if you screw something or restart the Kernel, up it's easy to recover your result. Turns out to be a huge time saver.

 

Back to David Curtin's homepage.