matplotlib动画内存泄漏

时间:2016-04-14 20:15:40

标签: python animation matplotlib memory-leaks

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

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

PagerAdapter

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

这个问题类似于Memory usage for matplotlib animation的问题,但没有回答。

我尝试在run函数中插入垃圾收集,但这没有帮助。我也尝试过使用python 2.7和python 3.5的matplotlib,结果相同。还有什么建议?预期会出现这种情况吗?

1 个答案:

答案 0 :(得分: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')