Pyplot savefig内存泄漏-替代方法?

时间:2019-06-01 15:04:57

标签: python matplotlib memory-leaks

是否有pyplot的替代方法可以在python中绘制一些图形? 我遇到了一些内存泄漏,这使我的项目无法完成。

我已经尝试了通常建议的“解决方案”,即使用plt.close(),plt.clf()等(带有或不带有单个fig = plt.figure()对象),但是它什么也没做。 >

我也尝试使用线程,但这是我第一次在python中使用它,所以我可能做错了:

import gc
import psutil
from threading import Thread
import matplotlib.pyplot as plt


def plot_func():
    plt.plot([1,2,3], [1,2,3])
    plt.savefig('plot_test.png')
    plt.close()

gc.collect()
init_mem_tot = psutil.Process().memory_info().rss / 2**20
for i in range(20):
    init_mem = psutil.Process().memory_info().rss / 2**20
    thread = Thread(target=plot_func)
    thread.start()
    thread.join()

    # tried with and without those the two following lines
    del thread
    gc.collect()
    new_mem = psutil.Process().memory_info().rss / 2**20
    print("Memory : %f, previous : %f. (%f difference)" % (new_mem, init_mem, init_mem - new_mem))

final_mem = psutil.Process().memory_info().rss / 2**20
print("Final : %f, Init : %f. (%f tot difference)" % (final_mem, init_mem_tot, final_mem - init_mem_tot))

结果给出:

Memory : 107.441406, previous : 102.687500. (4.753906 difference)
Memory : 108.578125, previous : 107.445312. (1.132812 difference)
Memory : 109.941406, previous : 108.578125. (1.363281 difference)
Memory : 111.281250, previous : 109.941406. (1.339844 difference)
Memory : 112.195312, previous : 111.281250. (0.914062 difference)
Memory : 113.281250, previous : 112.195312. (1.085938 difference)
Memory : 114.535156, previous : 113.281250. (1.253906 difference)
Memory : 115.640625, previous : 114.535156. (1.105469 difference)
Memory : 116.707031, previous : 115.640625. (1.066406 difference)
Memory : 117.843750, previous : 116.707031. (1.136719 difference)
Memory : 119.175781, previous : 117.843750. (1.332031 difference)
Memory : 120.367188, previous : 119.175781. (1.191406 difference)
Memory : 121.507812, previous : 120.367188. (1.140625 difference)
Memory : 122.660156, previous : 121.507812. (1.152344 difference)
Memory : 123.937500, previous : 122.660156. (1.277344 difference)
Memory : 124.828125, previous : 123.937500. (0.890625 difference)
Memory : 126.406250, previous : 124.832031. (1.574219 difference)
Memory : 127.671875, previous : 126.406250. (1.265625 difference)
Memory : 128.585938, previous : 127.671875. (0.914062 difference)
Memory : 129.699219, previous : 128.589844. (1.109375 difference)
Final : 129.699219, Init : 102.687500. (27.011719 tot difference)

如果这些都是解决方案,那么我还有其他方法可以绘制东西吗?

1 个答案:

答案 0 :(得分:0)

如果不使用线程和不使用pyplot,则只会观察到非常小的内存消耗增加。我不知道它来自哪里,但是也许可以忍受。

import gc
import psutil
from matplotlib.figure import Figure


def plot_func():
    fig = Figure()
    ax = fig.add_subplot()
    ax.plot([1,2,3], [1,2,3])
    fig.savefig('plot_test.png')

init_mem_tot = psutil.Process().memory_info().rss / 2**20
for i in range(100):
    init_mem = psutil.Process().memory_info().rss / 2**20

    plot_func()
    gc.collect()
    new_mem = psutil.Process().memory_info().rss / 2**20
    print("Memory : %f, previous : %f. (%f difference)" % (new_mem, init_mem, init_mem - new_mem))

final_mem = psutil.Process().memory_info().rss / 2**20
print("Final : %f, Init : %f. (%f tot difference)" % (final_mem, init_mem_tot, final_mem - init_mem_tot))

输出:(请注意第一张图的95%)

Memory : 48.113281, previous : 44.140625. (-3.972656 difference)
Memory : 48.152344, previous : 48.113281. (-0.039062 difference)
Memory : 48.195312, previous : 48.152344. (-0.042969 difference)
Memory : 48.199219, previous : 48.195312. (-0.003906 difference)
Memory : 48.199219, previous : 48.199219. (0.000000 difference)
Memory : 48.199219, previous : 48.199219. (0.000000 difference)
Memory : 48.257812, previous : 48.199219. (-0.058594 difference)
Memory : 48.257812, previous : 48.257812. (0.000000 difference)
Memory : 48.265625, previous : 48.257812. (-0.007812 difference)
Memory : 48.265625, previous : 48.265625. (0.000000 difference)
Memory : 48.269531, previous : 48.265625. (-0.003906 difference)
Memory : 48.273438, previous : 48.269531. (-0.003906 difference)
Memory : 48.273438, previous : 48.273438. (0.000000 difference)
Memory : 48.273438, previous : 48.273438. (0.000000 difference)
Memory : 48.273438, previous : 48.273438. (0.000000 difference)
Memory : 48.273438, previous : 48.273438. (0.000000 difference)
Memory : 48.281250, previous : 48.273438. (-0.007812 difference)
Memory : 48.285156, previous : 48.281250. (-0.003906 difference)
Memory : 48.285156, previous : 48.285156. (0.000000 difference)
Memory : 48.312500, previous : 48.285156. (-0.027344 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.312500, previous : 48.312500. (0.000000 difference)
Memory : 48.316406, previous : 48.312500. (-0.003906 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.316406, previous : 48.316406. (0.000000 difference)
Memory : 48.320312, previous : 48.316406. (-0.003906 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Memory : 48.320312, previous : 48.320312. (0.000000 difference)
Final : 48.320312, Init : 44.140625. (4.179688 tot difference)