【发布时间】:2015-11-23 15:38:42
【问题描述】:
EDIT/TL;DR: 看起来有一个 matplotlib.backends.backend_qt4.TimerQT 对象持有对我的 FuncAnimation 对象的引用。如何删除它以释放 FuncAnimation 对象?
1 - 一点上下文
我正在尝试对使用 matplotlib 生成的绘图进行动画处理。我使用 matplotlib.animation.FuncAnimation。 这个动画情节包含在一个FigureCanvasQTAgg(matplotlib.backends.backend_qt4agg)中,即。 PyQt4 小部件。
class ViewerWidget(FigureCanvasQTAgg):
def __init__(self, viewer, parent):
# viewer is my FuncAnimation, encapsulated in a class
self._viewer = viewer
FigureCanvasQTAgg.__init__(self, viewer.figure)
当 GUI 中的配置发生更改时,图形会被清除 (figure.clf()),其子图(轴和线)会被新的替换。
2 - 来自 Viewer 类的源代码(封装 FuncAnimation)
这是我的方法Viewer.show(...) 中最相关的部分,它实例化了 FuncAnimation
2.a - 首先,我尝试了:
animation.FuncAnimation(..., blit=True)
当然,实例立即被垃圾回收
2.b - 然后,我将它存储在一个类变量中:
self._anim = animation.FuncAnimation(..., blit=True)
它适用于第一个动画,但是一旦配置更改,我在新动画中都有来自以前动画的伪影
2.c - 所以我手动添加了del:
# Delete previous FuncAnimation if any
if self._anim:
del self._anim
self._anim = animation.FuncAnimation(..., blit=True)
没有改变
2.d - 经过一些调试,我检查了垃圾收集器:
# DEBUG: check garbage collector
def objects_by_id(id_):
for obj in gc.get_objects():
if id(obj) == id_:
return obj
self._id.remove(id_)
return "garbage collected"
# Delete previous FuncAnimation if any
if self._anim:
del self._anim
# DEBUG
print "-"*10
for i in self._id.copy():
print i, objects_by_id(i)
print "-"*10
self._anim = animation.FuncAnimation(self._figure_handler.figure,
update,
init_func=init,
interval=self._update_anim,
blit=True)
# DEBUG: store ids only, to enable object being garbage collected
self._anim_id.add(id(anim))
3次配置更改后,显示:
----------
140488264081616 <matplotlib.animation.FuncAnimation object at 0x7fc5f91360d0>
140488264169104 <matplotlib.animation.FuncAnimation object at 0x7fc5f914b690>
140488145151824 <matplotlib.animation.FuncAnimation object at 0x7fc5f1fca750>
140488262315984 <matplotlib.animation.FuncAnimation object at 0x7fc5f8f86fd0>
----------
因此,它确认 FuncAnimation 没有被垃圾回收
2.e - 最后一次尝试,使用弱引用:
# DEBUG: check garbage collector
def objects_by_id(id_):
for obj in gc.get_objects():
if id(obj) == id_:
return obj
self._id.remove(id_)
return "garbage collected"
# Delete previous FuncAnimation if any
if self._anim_ref:
anim = self._anim_ref()
del anim
# DEBUG
print "-"*10
for i in self._id.copy():
print i, objects_by_id(i)
print "-"*10
anim = animation.FuncAnimation(self._figure_handler.figure,
update,
init_func=init,
interval=self._update_anim,
blit=True)
self._anim_ref = weakref.ref(anim)
# DEBUG: store ids only, to enable object being garbage collected
self._id.add(id(anim))
这一次,日志令人困惑,我不确定发生了什么。
----------
140141921353872 <built-in method alignment>
----------
----------
140141921353872 <built-in method alignment>
140141920643152 Bbox('array([[ 0., 0.],\n [ 1., 1.]])')
----------
----------
140141921353872 <built-in method alignment>
140141920643152 <viewer.FftPlot object at 0x7f755565e850>
140141903645328 Bbox('array([[ 0., 0.],\n [ 1., 1.]])')
----------
(...)
我的<matplotlib.animation.FuncAnimation object at 0x...>呢?
没有更多以前的动画工件,到目前为止还不错,但是... FuncAnimation 不再能够执行“更新”。只有“初始化”部分。我的猜测是,一旦 Viewer.show(...) 方法返回,FuncAnimation 就会被垃圾回收,因为 anim ids 已经被回收了。
3 - 帮助
我不知道从这里往哪里看。有什么建议吗?
编辑: 我安装了objgraph 来可视化所有对 FuncAnimation 的反向引用,我得到了这个:
import objgraph, time
objgraph.show_backrefs([self._anim],
max_depth=5,
filename="/tmp/debug/func_graph_%d.png"
% int(time.time()))
所以,有一个 matplotlib.backends.backend_qt4.TimerQT 仍然持有引用。有什么办法去掉吗?
【问题讨论】:
-
我有点担心在 2b 中你走错了兔子洞来解决问题所在。如果你调用私有方法
anim._stop,它将从定时器回调中重新注册动画对象。 -
我不确定我是否理解在类变量中存储 FuncAnimation 对象有什么问题,但
anim._stop成功了!在实例化一个新的 FuncAnimation 之前,我在它上面调用了_stop(),现在,我没有更多的工件,我检查了垃圾收集器(与 2.d 相同)并且对象实际上是垃圾收集的。好吧,这可能不是调用私有方法的最干净的方法,但现在可以了。如果您从您的评论中做出回答,我会接受。谢谢!
标签: python-2.7 matplotlib garbage-collection pyqt4 blit