如何彻底清除所有Matplotlib图的内存 [英] How to clear memory completely of all Matplotlib plots

查看:152
本文介绍了如何彻底清除所有Matplotlib图的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据分析模块,该模块包含多次调用Matplotlib pyplot API的函数,每次运行最多生成30个图形.这些数字生成后会立即写入磁盘,因此我需要从内存中清除它们.目前,在每项功能结束时,我都会这样做

I have data analysis module that contains functions which call on Matplotlib pyplot API multiple times to generate up to 30 figures in each run. These figures get immediately written to disk after they are generated, and so I need to clear them from memory. Currently, at the end of each of my function, I do

import matplotlib.pyplot as plt

plt.clf()

但是,我不太确定此语句是否可以真正清除内存.我特别担心,因为我看到每次运行模块进行调试时,我的可用内存空间都会不断减少.每次将绘图写入磁盘后,有人可以请我启发我做些什么来真正清除内存吗?

however I'm not too sure if this statement can actually clear the memory. I'm especially concerned since I see that each time I run my module for debugging, my free memory space keeps decreasing. Can anyone please enlighten me on what I need to do to really clear my memory each time after I have written the plots to disk?

谢谢.

推荐答案

特别是在运行多个进程或线程时,最好定义图形变量并直接使用它:

Especially when you are running multiple processes or threads, it is much better to define your figure variable and work with it directly:

from matplotlib import pyplot as plt

f = plt.figure()
f.clear()
plt.close(f)

无论如何,您必须结合使用plt.clear()和plt.close()

In any case, you must combine the use of plt.clear() and plt.close()

这篇关于如何彻底清除所有Matplotlib图的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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