The problem I am about to ask has appeared once here: Memory Leak in Frontend - anyone know a workaround?. However, in this question I would like to put it in a bit different context.

Problem recipe:

  • Set $HistoryLength to 0 (this forces mathematica not to keep any history of evaluations)
  • Create a graphic object, which contains many colors/opacities/primitives/pointsizes/...
  • Open system monitor in your operational system
  • Rasterize the object in mathematica
  • Export it (optional), clear all the variables
  • Check your system monitor: memory used by mathematica frontend has grown larger

Example of the problem:

This example is not minimal, but is aesthetically appealing. Following the recipe, first set the history length:

$HistoryLength = 0; 

Then create a graphic object. In this case, it is a set of approx 0.5 10^4 points, having random positions inside a 0.5 radius sphere, random sizes and random colors, following some colorscheme. A cylinder is added afterwards to make the object look like a tree.

PointNumber = 10^4;
BushSize = 0.5;
PointLocations = 
  Select[(2 BushSize {Random[], Random[], Random[]} - {0.5, 0.5, 
        0.5}) & /@ Range[PointNumber], Norm[#] < BushSize &];
PointNumber2 = Length[PointLocations];
PointSizes = 0.01 Random[] & /@ Range[PointNumber2];
PointColors = 
  ColorData["PlumColors"][Random[]] & /@ Range[PointNumber2];
GraphicObject = 
  MapThread[{#1, PointSize[#2], Point[#3]} &, {PointColors, 
    PointSizes, PointLocations}];

Now, open the system monitor and rasterize everything:

TreeStuff = 
  Graphics3D[{Opacity[0.8], Brown, EdgeForm[], 
    Cylinder[{{0, 0, -1}, {0, 0, 0.1}}, 0.01], GraphicObject}, 
   PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}}, ImageSize -> 600, 
   Boxed -> False]]

As you can see, mathematica front-end has expanded in size by some 50 Mb! It is not all, at all. If you reevaluate the notebook, mathematica will pick another set of random points and will expand in the memory again. However, if you re-rasterize an object which has been rasterized before, memory will not be leaked.


The described behavior is clearly a bug. Say, one is producing an animation. If a given frame has been rasterized and saved, no information needs to be stored about it in the memory. Nevertheless, it gets stored incrementally.

It seems hard to imagine, that nobody has ever created complex graphics with mathematica before. In particular, in processing datasets or in creating animations, one has to rasterize such graphics many times. In this case, if the parameters of graphics change, mathematica takes all the system memory. Therefore, at least somebody, making complex graphics with mathematica, must have faced a similar problem before.

What do people commonly do to treat this issue? How can one clear the frontend memory, which expands so obviously in system monitor after each new evaluation of the above presented code?


The code outputs this pretty tree: enter image description here

  • $\begingroup$ Beautiful tree indeed :) $\endgroup$
    – Öskå
    Sep 15, 2013 at 22:10
  • $\begingroup$ Thank you! In fact, it is rather easy to generalize the code, so that it generates a grove of a given shape, with grass and clouds. $\endgroup$ Sep 15, 2013 at 22:12
  • $\begingroup$ @AlexeyBobrick Or you can use built-in stuff to create a stupid scene :P $\endgroup$
    – rm -rf
    Sep 16, 2013 at 2:06
  • $\begingroup$ You're right, other people do have problems with producing large graphics. :) Sometimes they're mentioned on the site (eg this Q&A). Other users report possible memory leaks. $\endgroup$
    – cormullion
    Sep 16, 2013 at 8:42
  • $\begingroup$ @rm-rf : I once were making galactic dynamical simulations, where I put Beethoven into the center. Looked rather inspiring. $\endgroup$ Sep 16, 2013 at 11:42


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.