Pyplot Savefig Memory Leak - Alternative?
Is there an alternative to pyplot to plot some graphs in python? I'm having some memory leaks which make my project impossible to complete. I've tried the usual recommended 'soluti
Solution 1:
If not using threading and not using pyplot, only a very slight memory consumption increase is observed. I do not know where it comes from, but it might be bearable.
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**20plot_func()
gc.collect()
new_mem = psutil.Process().memory_info().rss / 2**20print("Memory : %f, previous : %f. (%f difference)" % (new_mem, init_mem, init_mem - new_mem))
final_mem = psutil.Process().memory_info().rss / 2**20print("Final : %f, Init : %f. (%f tot difference)" % (final_mem, init_mem_tot, final_mem - init_mem_tot))
Output: (Note how 95% are by the very first plot)
Memory :48.113281,previous :44.140625.(-3.972656difference)Memory :48.152344,previous :48.113281.(-0.039062difference)Memory :48.195312,previous :48.152344.(-0.042969difference)Memory :48.199219,previous :48.195312.(-0.003906difference)Memory :48.199219,previous :48.199219.(0.000000difference)Memory :48.199219,previous :48.199219.(0.000000difference)Memory :48.257812,previous :48.199219.(-0.058594difference)Memory :48.257812,previous :48.257812.(0.000000difference)Memory :48.265625,previous :48.257812.(-0.007812difference)Memory :48.265625,previous :48.265625.(0.000000difference)Memory :48.269531,previous :48.265625.(-0.003906difference)Memory :48.273438,previous :48.269531.(-0.003906difference)Memory :48.273438,previous :48.273438.(0.000000difference)Memory :48.273438,previous :48.273438.(0.000000difference)Memory :48.273438,previous :48.273438.(0.000000difference)Memory :48.273438,previous :48.273438.(0.000000difference)Memory :48.281250,previous :48.273438.(-0.007812difference)Memory :48.285156,previous :48.281250.(-0.003906difference)Memory :48.285156,previous :48.285156.(0.000000difference)Memory :48.312500,previous :48.285156.(-0.027344difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.312500,previous :48.312500.(0.000000difference)Memory :48.316406,previous :48.312500.(-0.003906difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.316406,previous :48.316406.(0.000000difference)Memory :48.320312,previous :48.316406.(-0.003906difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Memory :48.320312,previous :48.320312.(0.000000difference)Final :48.320312,Init :44.140625.(4.179688totdifference)
Post a Comment for "Pyplot Savefig Memory Leak - Alternative?"