Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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… –  F'x Mar 29 '12 at 20:02
    
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? –  Lou Mar 29 '12 at 20:46
    
Sleep is not a Mathematica command. I think you mean Pause. I seem to have the reported behavior too (Win7-64/mma8.04). –  Sjoerd C. de Vries Mar 29 '12 at 21:27
    
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! –  Lou Mar 30 '12 at 8:38
add comment

3 Answers

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.

share|improve this answer
    
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. –  halirutan Mar 30 '12 at 2:21
    
Btw, if you have Windows, can you try whether my work-around fixes the leak on your machine? –  halirutan Mar 30 '12 at 2:22
2  
@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. –  Yu-Sung Chang Mar 30 '12 at 14:42
add comment

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

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

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

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

share|improve this answer
4  
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? –  Albert Retey Mar 30 '12 at 9:12
3  
@AlbertRetey Feel free to report it as a bug through tech support. Additional external reporters adds weight to a report. –  Brett Champion Mar 31 '12 at 2:55
add comment

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"].

Work-around

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:

Do[
  Block[{x, y, c},
   {x, y, c} = RandomReal[{0, 1}, 3];
   img = Rasterize[
     Graphics[
      Table[{ColorData["TemperatureMap"][c], 
        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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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