matplotlib动画内存泄漏 [英] matplotlib animate memory leak

查看:67
本文介绍了matplotlib动画内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用matplotlib动画功能来显示一些实时数据.运行几分钟后,我注意到python所使用的内存不断增加.我决定返回一些动画示例,看看它们在我的计算机上是否有相同的问题.

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.

当我使用 animate_decay.py 示例并将重复从False更改为True时,我可以模拟我的实时数据程序显示的相同问题.这是一个更改的代码.

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()

我正在使用Mac(OS X 10.10),并通过Activity Monitor观看python使用的内存.随着动画一遍又一遍的重复,python正在获取越来越多的内存.几分钟后,python正在使用300Mb以上的内存.

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.

此问题类似于以下问题: matplotlib动画的内存使用情况但是没有得到答案.

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

我尝试在run函数中插入垃圾回收,但这没有帮助.我还尝试了python 2.7和python 3.5的matplotlib,结果相同.还有其他建议吗?这是预期的行为吗?

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?

推荐答案

因此,问题是在Mac上matplotlib使用的默认后端.默认后端可以通过以下方式找到:

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()

在我的迷你Mac(OS 10.10)上,它是'MacOSX'

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

切换到Qt4Agg或TkAgg都可以.将这两行插入上一代码的顶部.

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

import matplotlib
matplotlib.use('TkAgg')

这篇关于matplotlib动画内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆