How can I release memory after creating matplotlib figures

后端 未结 2 1169
误落风尘
误落风尘 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:32

    Did you try to run you task function several times (in a for) to be sure that not your function is leaking no matter of celery? Make sure that django.settings.DEBUG is set False( The connection object holds all queries in memmory when DEBUG=True).

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题