Skip to content

Memory leakage while plotting in a loop

Memory leakage while plotting in a loop

Contents

Issue

Memory leakage while generating python matplotlib plots in a loop on MacOS system.

I was using python 3.9 and MacOS Catalina.

 

I was trying to generate lots of plots for my analysis. Idea was to create them in a loop:

  • render plot
  • save the output
  • iterate further

Simple example of the case:

However, it turned out that this simple code quickly led to memory overflow, despite closing the figure object on each iteration.

 

Attempts

On the first attempt I tried cleaning up the figure object and making sure that all of the figures are indeed closed.

That did not help, so I tried to enforce garbage collection to explicitly free up the memory.

No success either.

 

Solution

This time I encountered an option to change the matplotlib backendmatplotlib.use(“Agg”).

And that helped!

What does that mean to set the plotting backend?

As quoting the documentation:

There are two types of backends for matplotlib: user interface backends, also referred to as “interactive backends” and hardcopy backends to make image files, referred to as “non-interactive backends”.

That basically means that in order to save files, as in my case, it was better to use a dedicated matplotlib backend instead of the default one – which is interactive. Matplotlib should in principle automatically select proper backend based on the given use case, however as documentation states, it is not always the case.

Selected “Agg” backend refers to png file type.

6 thoughts on “Memory leakage while plotting in a loop

Leave a Reply

Your email address will not be published. Required fields are marked *