Memory leak when creating matplotlib.plots in loops

Viewed 16

I know it was discussed not only once. Unfortunately, I do not get further.

first the basic: I am trying to loop through OHLC data and create charts. Unfortunately the memory keeps running full.

Apparently it has something to do with the size of the chart.

plt.ioff()
fig = plt.figure(figsize=(50, 25))

for line in ohlc_data:

   # Make magic with the data
   # ...

   df = x.get_ohlc_data()
   ohlc_area = plt.subplot2grid((6, 3), (0, 0), colspan=3, rowspan=4)
   ohlc_area.plot(df[['Date']], df[['close']], 'k-', markevery=markers_on, marker='D',
                                                       label='close')
   plt.grid(axis='x', color='0.95')
   plt.grid(axis='y', color='0.95')
   plt.savefig("Diagramme/" + str(x.id) + "_" + x.state + ".png")

   # Try to clean up everything
   fig.clf()
   plt.cla()
   plt.clf()
   plt.close('all')
   gc.collect()

Up to this point it works. Unfortunately fig.clf() also resets the size of the diagram. If I try to resize the diagram within the loop, there is a memory leak and I don't understand why.

plt.ioff()
fig = plt.figure(figsize=(50, 25))

for line in ohlc_data:

   # Make magic with the data
   # ...

   #plt.figure(figsize=(50, 25))
   plt.rcParams["figure.figsize"] = (50, 25)
   #fig = plt.figure(figsize=(50, 25))

   df = x.get_ohlc_data()
   ohlc_area = plt.subplot2grid((6, 3), (0, 0), colspan=3, rowspan=4)
   ohlc_area.plot(df[['Date']], df[['close']], 'k-', markevery=markers_on, marker='D',
                                                       label='close')
   plt.grid(axis='x', color='0.95')
   plt.grid(axis='y', color='0.95')
   plt.savefig("Diagramme/" + str(x.id) + "_" + x.state + ".png")

   # Try to clean up everything
   fig.clf()
   plt.cla()
   plt.clf()
   plt.close('all')
   gc.collect()

I tried at the beginning of the loop with:

   #plt.figure(figsize=(50, 25))
   plt.rcParams["figure.figsize"] = (50, 25)
   #fig = plt.figure(figsize=(50, 25))

Unfortunately it does not work, can someone tell me how I can prevent the memory increase or how this problem comes about?

Thanks in advance

0 Answers
Related