When using Rasterize on a Graphics expression composed of many differently coloured Graphics objects memory is allocated in the frontend which is then not released or reused when a similar graphic object is created.

Rasterize (or its underlying machinery) is used underneath Export when a graphic object is exported using Export, for instance when creating a jpg file.

In the example given 4 MB of memory is allocated each cycle but not freed. Eventually the frontend will crash.

Do[Sleep[3]; img = Rasterize[Graphics[Table[
  { ColorData["TemperatureMap"][RandomReal[]], Circle[{RandomReal[], RandomReal[]}, 1.0] },
   {1000}]]], {1000}]; 

Does anyone know if this is a known bug and if there are any workarounds pending a fix from Wolfram?

  • $\begingroup$ What system/OS are you using, which version of Mathematica? If I run your loop on M8.0.0.0 on Mac OS 10.7.3, I don't see any memory usage increasing, and nothing crashes… $\endgroup$ – F'x Mar 29 '12 at 20:02
  • $\begingroup$ I have the same results as described. running Windows7 with MM8.0.4.0. or with MM7.01. When you clear img it seems only the last img is cleared and the rest stays in the cache? $\endgroup$ – Lou Mar 29 '12 at 20:46
  • $\begingroup$ Sleep is not a Mathematica command. I think you mean Pause. I seem to have the reported behavior too (Win7-64/mma8.04). $\endgroup$ – Sjoerd C. de Vries Mar 29 '12 at 21:27
  • $\begingroup$ It seems that Haliruntan is right about the RandomReal but it's only causing trouble when used within the ColorData function. When you use ColorData["TemperatureMap"][RandomReal[]] the problem shows. When you use ColorData["TemperatureMap"][0.5] the problem goes away! $\endgroup$ – Lou Mar 30 '12 at 8:38

This is a simpler example that shows the behavior (in Win-x86-64, M8.0.4):

Do[Rasterize[Graphics[Table[RGBColor @@ RandomReal[1, {3}], {1000}]]], {1000}]

Apparently, the memory gets allocated for each RGBColor with unique value, but is never freed. The same goes to other color directives.

  • $\begingroup$ The thing I don't understand is, what has Rasterize to do with it? When Rasterize gets evaluated, the random color is already inside the Graphics object. Therefore, without rasterizing the graphics, the same leak should appear, which obviously doesn't. $\endgroup$ – halirutan Mar 30 '12 at 2:21
  • $\begingroup$ Btw, if you have Windows, can you try whether my work-around fixes the leak on your machine? $\endgroup$ – halirutan Mar 30 '12 at 2:22
  • 2
    $\begingroup$ @halirutan: Yours does fix it, because it is essentially using only one color. The problem here is that whenever new color directive is introduced, FE allocates memory which never get freed. The original question creates 1000 x 1000 color directives. $\endgroup$ – Yu-Sung Chang Mar 30 '12 at 14:42

The workaround is to limit the number of styles, say by rounding the random numbers:

random[] := Round[RandomReal[], 0.01]

   img = Rasterize[Graphics[
          Circle[{random[], random[]}, 1.0]},

This won't remove the memory leak, but will at least bound it.

  • 5
    $\begingroup$ Would WRI consider this to be a bug and can we expect it to be fixed in future versions? Has this been reported as a bug? $\endgroup$ – Albert Retey Mar 30 '12 at 9:12
  • 3
    $\begingroup$ @AlbertRetey Feel free to report it as a bug through tech support. Additional external reporters adds weight to a report. $\endgroup$ – Brett Champion Mar 31 '12 at 2:55

First thing to notice, that using Image instead of Rasterize has the same behavior. Maybe internally it is just a call to Rasterize[img,"Image"].


The second and more disturbing thing is, that it seems to be RandomReal[] in combination with Rasterize which opens the leak. Changing the code a bit prevents this behavior:

  Block[{x, y, c},
   {x, y, c} = RandomReal[{0, 1}, 3];
   img = Rasterize[
        Circle[{x, y}, 1.0]}, {1000}]]]
   ], {1000}];

In fact, if you play a bit with this, then you notice, that the most evil part was the RandomReal[] inside the ColorData.

This was tested on Linux-x86-64 with V8.0.4.

  • 1
    $\begingroup$ Apparently this strange bug still exists in version 11.1.1 (after 5 years!). Even when using RandomInteger or RandomChoice within Rasterize. Moving the random number generation outside the function works. $\endgroup$ – dan7geo May 30 '17 at 3:11

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.