3

I have been using the matplotlib animation functions to display some real-time data. After running for a few minutes I noticed the memory used by python kept creeping up and up. I decided to go back to some of the animation examples and see if they have the same problem on my computer.

When I use the animate_decay.py example and change repeat from False to True, I can simulate the same problem my real-time data program exhibits. Here is the code with the one change.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def data_gen(t=0):
    cnt = 0
    while cnt < 1000:
        cnt += 1
        t += 0.1
        yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)

def init():
    ax.set_ylim(-1.1, 1.1)
    ax.set_xlim(0, 10)
    del xdata[:]
    del ydata[:]
    line.set_data(xdata, ydata)
    return line,

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.grid()
xdata, ydata = [], []


def run(data):
    # update the data
    t, y = data
    xdata.append(t)
    ydata.append(y)
    xmin, xmax = ax.get_xlim()

    if t >= xmax:
        ax.set_xlim(xmin, 2*xmax)
        ax.figure.canvas.draw()
    line.set_data(xdata, ydata)

    return line,

ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,
                              repeat=True, init_func=init, save_count=0)
plt.show()

I'm using a Mac (OS X 10.10) and watch the memory python uses via Activity Monitor. As the animation repeats over and over again, python is grabbing more and more memory. After several minutes python was using more than 300Mb.

This problem is similar to the question at: Memory usage for matplotlib animation But was not answered.

I have tried inserting garbage collection in the run function, but this did not help. I have also tried matplotlib with python 2.7 and python 3.5 with the same results. Any further suggestions? Is this behavior expected?

4
  • I'm running your code unaltered in an Ipython notebook on Win10, Python 2.7, Anaconda 64 bit , and I see the process using 87.1 MB, with slight fluctuations, but always coming back got the same number during first few minutes. After 10+ minutes at 87.6 MB. Commented Apr 14, 2016 at 20:31
  • Strange. I have the memory problem regardless of how I run the code. Pasted into python, ipython notebook, or command line. I'm also using anaconda 4.0. Maybe it is something to do with the default graphics? Commented Apr 14, 2016 at 21:17
  • Win7, Anaconda 3.11.0-dirty, Ipython Notebook 2.7 with the Qt4Agg backend went from 68.8MB to 69.5MB in 15 minutes or so. Can't reproduce the issue. Commented Apr 15, 2016 at 0:08
  • Thanks ljetibo and @roadrunner66 for trying to reproduce this problem. The default backend on the Mac is the problem. I tried Qt4Agg and TkAgg, both work fine! Commented Apr 15, 2016 at 15:13

1 Answer 1

2

So the problem is the default backend used by matplotlib on a Mac. The default backend can be found by:

import matplotlib
matplotlib.get_backend()

On my mini Mac (OS 10.10) it is 'MacOSX'

Switching to either Qt4Agg or TkAgg works fine. Insert these two lines at the top of the previous code.

import matplotlib
matplotlib.use('TkAgg')
Sign up to request clarification or add additional context in comments.

2 Comments

Did that really fix the memory consumption issue?
I am also curious, seem to have the same problem on win10

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.