This is apparently a bug. In case this question sticks around, the following modifications to the code eliminated the memory leak on my machine:
import sys
import gc
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import pandas as pd
pdindex = pd.date_range(start = '01/01/2013', freq = '15min', end = '01/01/2019')
df = pd.DataFrame({
'test': np.linspace(0, 1, len(pdindex))
}, index = pdindex)
fig, ax = plt.subplots()
def memplot_plot(df, i, f, a):
a.plot(df.index, df.test)
a.set_title('graph' + str(i))
f.savefig(str(i) + '.png', dpi = 144)
a.cla()
for i in range(1, 100):
print '*******************************'
print 'i : ' + str(i)
print len(gc.get_objects())
print sys.getsizeof(gc.get_objects())
memplot_plot(df, i, fig, ax)
gc.collect()
The output now looks like this:
...
**
** ** ** ** ** ** ** ** ** ** ** ** ** ** *
i: 13
83727
732816
**
** ** ** ** ** ** ** ** ** ** ** ** ** ** *
i: 14
83727
732816
**
** ** ** ** ** ** ** ** ** ** ** ** ** ** *
i: 15
83727
732816
I have to plot and saveplot in a loop from 1 to 500 with different data but gives a memory leak due to Matplot lib. Has someone any idea on how to deal with that ?,This is apparently a bug. In case this question sticks around, the following modifications to the code eliminated the memory leak on my machine:, 1 week ago Dec 14, 2020 · Sun Dec 13, 2020 11:40 am. Hello, I am experiencing a python memory leak when using tkinter and matplotlib.pyplot. I am a beginner with Python. Back ground: The Python project I'm developing is for the purpose of monitoring a friends farm. Last year during the drought in Australia the farm, located near Goldburn, NSW ran out of water and feed. , 1 week ago Feb 13, 2015 · memory leaks using matplotlib. Ask Question. 5. This is not intended as a bug report--even if these leaks may be a result of mpl bugs, please interpret the question ask asking for a way around them. The problem is simple: plot a large chunk of data (using plot () or scatter ()), clear/release everything, garbage collect, but still not nearly ...
import sys
import gc
import numpy as np
import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt
import pandas as pd pdindex = pd.date_range(start = '01/01/2013', freq = '15min', end = '01/01/2019') df = pd.DataFrame({
'test': np.random.normal(0, 1, len(pdindex))
}, index = pdindex) def memplot_plot(df, i): df.test.plot() plt.title('graph' + str(i)) plt.savefig(str(i) + '.png', dpi = 144) plt.close() for i in range(1, 100): print '*******************************'
print 'i : ' + str(i) print len(gc.get_objects()) print sys.getsizeof(gc.get_objects()) memplot_plot(df, i) gc.collect()
import sys
import gc
import numpy as np
import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt
import pandas as pd pdindex = pd.date_range(start = '01/01/2013', freq = '15min', end = '01/01/2019') df = pd.DataFrame({
'test': np.linspace(0, 1, len(pdindex))
}, index = pdindex) fig, ax = plt.subplots() def memplot_plot(df, i, f, a): a.plot(df.index, df.test) a.set_title('graph' + str(i)) f.savefig(str(i) + '.png', dpi = 144) a.cla() for i in range(1, 100): print '*******************************'
print 'i : ' + str(i) print len(gc.get_objects()) print sys.getsizeof(gc.get_objects()) memplot_plot(df, i, fig, ax) gc.collect()
import sys
import gc
import numpy as np
import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt
import pandas as pd pdindex = pd.date_range(start = '01/01/2013', freq = '15min', end = '01/01/2019') df = pd.DataFrame({
'test': np.random.normal(0, 1, len(pdindex))
}, index = pdindex) def memplot_plot(df, i): df.test.plot() plt.title('graph' + str(i)) plt.savefig(str(i) + '.png', dpi = 144) plt.close() for i in range(1, 100): print '*******************************'
print 'i : ' + str(i) print len(gc.get_objects()) print sys.getsizeof(gc.get_objects()) memplot_plot(df, i) gc.collect()
import sys
import gc
import numpy as np
import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt
import pandas as pd pdindex = pd.date_range(start = '01/01/2013', freq = '15min', end = '01/01/2019') df = pd.DataFrame({
'test': np.linspace(0, 1, len(pdindex))
}, index = pdindex) fig, ax = plt.subplots() def memplot_plot(df, i, f, a): a.plot(df.index, df.test) a.set_title('graph' + str(i)) f.savefig(str(i) + '.png', dpi = 144) a.cla() for i in range(1, 100): print '*******************************'
print 'i : ' + str(i) print len(gc.get_objects()) print sys.getsizeof(gc.get_objects()) memplot_plot(df, i, fig, ax) gc.collect()
... ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * i: 13 83727 732816 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * i: 14 83727 732816 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** * i: 15 83727 732816
Last Updated : 01 Dec, 2021,GATE CS 2021 Syllabus
Output:
No.of tracked objects before calling get method
16071
Status code 200
No.of tracked objects after calling get method
16158