How can I release memory after creating matplotlib figures

后端 未结 2 1185
误落风尘
误落风尘 2020-11-29 19:08

I have several matlpotlib functions rolled into some django-celery tasks.

Every time the tasks are called more RAM is dedicated to python. Before too long, python is

2条回答
  •  天命终不由人
    2020-11-29 19:35

    import matplotlib.pyplot as plt
    from datetime import datetime
    import gc
    
    class MyClass:
        def plotmanytimesandsave(self):
            plt.plot([1, 2, 3])
            ro2 = datetime.now()
            f =ro2.second
            name =str(f)+".jpg"
            plt.savefig(name)
            plt.draw()
            plt.clf()
            plt.close("all")
    
    
    for y in range(1, 10):
        k = MyClass()
        k.plotmanytimesandsave()
        del k
        k = "now our class object is a string"
        print(k)
        del k
        gc.collect
    

    with this program you will save directly as many times you want without the plt.show() command. And the memory consumption will be low.

提交回复
热议问题