Python 从地址中删除matplotlib Line2D对象

Python 从地址中删除matplotlib Line2D对象,python,matplotlib,memory,garbage-collection,Python,Matplotlib,Memory,Garbage Collection,我目前正在尝试提高脚本的内存使用率,该脚本会随着时间的推移生成非常“沉重”的图形 在创建图形之前: ('Before:heap:',一组337个对象的分区。总大小=82832字节 索引计数%Size%累计%Kind(类/类的目录) ) 在创建它们之后: ('After:heap:',一组89339个对象的分区。总大小=32584064字节 索引计数%Size%累计%Kind(类/类的目录) ) 那么,我会: figures=[manager.canvas.figure for manager i

我目前正在尝试提高脚本的内存使用率,该脚本会随着时间的推移生成非常“沉重”的图形

在创建图形之前:

('Before:heap:',一组337个对象的分区。总大小=82832字节

索引计数%Size%累计%Kind(类/类的目录)

)

在创建它们之后:

('After:heap:',一组89339个对象的分区。总大小=32584064字节

索引计数%Size%累计%Kind(类/类的目录)

)

那么,我会:

figures=[manager.canvas.figure for manager in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
for i, figure in enumerate(figures): figure.clf(); plt.close(figure)
figures=[manager.canvas.figure for manager in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]#here, figures==[]
del figures
hp.heap()
这张照片是:

一组71966个对象的分区。总大小=23491976字节

索引计数%Size%累计%Kind(类/类的目录)

因此,似乎有少数matplotlib对象已被删除,但并非全部

首先,我想查看剩下的所有Line2D对象:

objs = [obj for obj in gc.get_objects() if isinstance(obj, matplotlib.lines.Line2D)]
#[... very long list with e.g., <matplotlib.lines.Line2D object at 0x1375ede590>, <matplotlib.lines.Line2D object at 0x1375ede4d0>, <matplotlib.lines.Line2D object at 0x1375eec390>, <matplotlib.lines.Line2D object at 0x1375ef6350>, <matplotlib.lines.Line2D object at 0x1375eece10>, <matplotlib.lines.Line2D object at 0x1375eec690>, <matplotlib.lines.Line2D object at 0x1375eec610>, <matplotlib.lines.Line2D object at 0x1375eec590>, <matplotlib.lines.Line2D object at 0x1375eecb10>, <matplotlib.lines.Line2D object at 0x1375ef6850>, <matplotlib.lines.Line2D object at 0x1375eec350>]
print len(objs)#29199 (!!!)
objs=[gc.get_objects()中obj的obj如果是instance(obj,matplotlib.lines.Line2D)]
#[…非常长的列表,例如,,,,,]
打印长度(objs)#29199(!!!)
所以现在我希望能够访问所有这些对象,以便能够删除它们并清除内存,但我不知道如何才能做到这一点


谢谢你的帮助

objs:del obj中obj的
结果是什么?它从列表中删除项目,而不是内存中的对象。
figures=[manager.canvas.figure for manager in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
for i, figure in enumerate(figures): figure.clf(); plt.close(figure)
figures=[manager.canvas.figure for manager in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]#here, figures==[]
del figures
hp.heap()
 0   1581   2  5299512  23   5299512  23 dict of matplotlib.lines.Line2D
 1   1063   1  3563176  15   8862688  38 dict of matplotlib.text.Text
 2   7337  10  2356952  10  11219640  48 dict (no owner)
 3   1584   2  1660032   7  12879672  55 dict of matplotlib.path.Path
 4   1581   2  1656888   7  14536560  62 dict of matplotlib.markers.MarkerStyle
 5    441   1  1478232   6  16014792  68 dict of matplotlib.axis.XTick
 6   1063   1  1114024   5  17128816  73 dict of matplotlib.font_manager.FontProperties
 7   7583  11   619384   3  17748200  76 list
 8   6500   9   572000   2  18320200  78 __builtin__.weakref
 9   6479   9   518320   2  18838520  80 numpy.ndarray
objs = [obj for obj in gc.get_objects() if isinstance(obj, matplotlib.lines.Line2D)]
#[... very long list with e.g., <matplotlib.lines.Line2D object at 0x1375ede590>, <matplotlib.lines.Line2D object at 0x1375ede4d0>, <matplotlib.lines.Line2D object at 0x1375eec390>, <matplotlib.lines.Line2D object at 0x1375ef6350>, <matplotlib.lines.Line2D object at 0x1375eece10>, <matplotlib.lines.Line2D object at 0x1375eec690>, <matplotlib.lines.Line2D object at 0x1375eec610>, <matplotlib.lines.Line2D object at 0x1375eec590>, <matplotlib.lines.Line2D object at 0x1375eecb10>, <matplotlib.lines.Line2D object at 0x1375ef6850>, <matplotlib.lines.Line2D object at 0x1375eec350>]
print len(objs)#29199 (!!!)