Is there any way to profile the mathkernel memory usage (down to individual variables) other than paying $$$ for their Eclipse plugin (mathematica workbench, iirc)?
Right now I finish execution of a program that takes multiple GB's of ram, but the only things that are stored should be ~50MB of data at most, yet mathkernel.exe tends to hold onto ~1.5GB (basically, as much as Windows will give it). Is there any better way to get around this, other than saving the data I need and quitting the kernel every time?
EDIT: I've just learned of the ByteCount function (which shows some disturbing results on basic datatypes, but that's besides the point), but even the sum over all my variables is nowhere near the amount taken by mathkernel. What gives?
Here is my solution for profiling of memory usage:
Evaluation the above gives the following table:
One thing a lot of users don't realize is that it takes memory to store all your inputs and outputs in the
In
andOut
symbols, regardless of whether or not you assign an output to a variable.Out
is also aliased as%
, where%
is the previous output,%%
is the second-to-last, etc.%123
is equivalent toOut[123]
.If you don't have a habit of using
%
, or only use it to a few levels deep, set$HistoryLength
to 0 or a small positive integer, to keep only the last few (or no) outputs around inOut
.You might also want to look at the functions
MaxMemoryUsed
andMemoryInUse
.Of course, the
$HistoryLength
issue may or not be your problem, but you haven't shared what your actual evaluation is. If you're able to post it, perhaps someone will be able to shed more light on why it's so memory-intensive.One way is to automatize restarting of kernel when it goes out of memory. You can execute your memory-consuming code in a slave kernel while the master kernel only takes the result of computation and controls memory usage.
Michael Pilat's answer is a good one, and
MemoryInUse
andMaxMemoryUsed
are probably the best tools you have.ByteCount
is rarely all that helpful because what it measures can be a huge overestimate because it ignores shared subexpressions, and it often ignores memory that isn't directly accessible through Mathematica functions, which is often a major component of memory usage.One thing you can do in some circumstances is use the
Share
function, which forces subexpressions to be shared when possible. In some circumstances, this can save you tens or even hundreds of magabytes. You can tell how well it's working by usingMemoryInUse
before and after you useShare
.Also, some innocuous-seeming things can cause Mathematica to use a whole lot more memory than you expect. Contiguous arrays of machine reals (and only machine reals) can be allocated as so-called "packed" arrays, much the way they would be allocated by C or Fortran. However, if you have a mix of machine reals and other structures (including symbols) in an array, everything has to be "boxed", and the array becomes an array of pointers, which can add a lot of overhead.