Why does Matplotlib leak memory when generating many plots in a loop, and how do you prevent it?

Quick Overview

Asks why a Python script that creates many Matplotlib figures in a loop keeps growing in memory and how to stop it. Tests understanding of how Matplotlib manages figure lifetime, the pyplot and object-oriented interfaces, backend choice for batch rendering, and how to verify a fix.

Why does Matplotlib leak memory when generating many plots in a loop, and how do you prevent it?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Why can Matplotlib sometimes lead to memory leaks, especially when generating a large number of plots in a loop, and what are the key strategies to prevent them? A typical case is a batch job that saves one PNG per dataset and keeps growing in memory until it is killed. ```hint Follow the references Ask what still holds a reference to each figure after `savefig()` returns. ``` ### What a Strong Answer Covers - Where the references to old figures live and why that keeps them from being garbage-collected - Why saving, clearing or deleting a figure variable does not release it - Concrete ways to release figures, or avoid accumulating them, inside a loop - The role of the backend in batch rendering - How to confirm the fix ### Follow-up Questions - What is the difference between `plt.close(fig)`, `fig.clf()` and `ax.cla()`? - How can you create and save a figure without pyplot registering it at all? - Memory still creeps up after you add `plt.close(fig)`. What else could be holding on to figure data? - How would you generate 10,000 plots in parallel without running out of memory?

Overview: Asks why a Python script that creates many Matplotlib figures in a loop keeps growing in memory and how to stop it. Tests understanding of how Matplotlib manages figure lifetime, the pyplot and object-oriented interfaces, backend choice for batch rendering, and how to verify a fix.

PracHub logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Why can Matplotlib sometimes lead to memory leaks, especially when generating a large number of plots in a loop, and what are the key strategies to prevent them? A typical case is a batch job that saves one PNG per dataset and keeps growing in memory until it is killed.

What a Strong Answer Covers Guidance

  • Where the references to old figures live and why that keeps them from being garbage-collected
  • Why saving, clearing or deleting a figure variable does not release it
  • Concrete ways to release figures, or avoid accumulating them, inside a loop
  • The role of the backend in batch rendering
  • How to confirm the fix

Follow-up Questions Guidance

  • What is the difference between plt.close(fig) , fig.clf() and ax.cla() ?
  • How can you create and save a figure without pyplot registering it at all?
  • Memory still creeps up after you add plt.close(fig) . What else could be holding on to figure data?
  • How would you generate 10,000 plots in parallel without running out of memory?