简体   繁体   English

在循环中创建 matplotlib.plots 时 Memory 泄漏

[英]Memory leak when creating matplotlib.plots in loops

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.首先是基本的:我正在尝试遍历 OHLC 数据并创建图表。 Unfortunately the memory keeps running full.不幸的是,memory 一直满载运行。

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.不幸的是 fig.clf() 也重置了图表的大小。 If I try to resize the diagram within the loop, there is a memory leak and I don't understand why.如果我尝试在循环中调整图表大小,则会出现 memory 泄漏,我不明白为什么。

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?不幸的是它不起作用,有人可以告诉我如何防止 memory 增加或这个问题是如何产生的?

Thanks in advance提前致谢

Not sure if this helps, but perhaps moves you in the right direction.不确定这是否有帮助,但可能会让你朝着正确的方向前进。

You are creating a new plot axis during each for loop iteration with the call to subplot2grid.您正在通过调用 subplot2grid 在每次 for 循环迭代期间创建一个新的 plot 轴。 Move this outside of the loop completely:将其完全移出循环:

plt.ioff()
fig = plt.figure(figsize=(50, 25))
ohlc_area = plt.subplot2grid((6, 3), (0, 0), colspan=3, rowspan=4)

for line in ohlc_data:

   # Make magic with the data
   # ...


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

   # remove the other lines where you were attempting garbage cleanup... the ohlc_area.clear() should clean the axis during each iteration so you can make and save a new plot ... 

Also, you can remove all the garbage cleanup stuff/此外,您可以删除所有垃圾清理的东西/

Note, there are a lot of unknowns for someone reading your code, for example df = x.get_ohlc_data() , I have no idea what thats doing, and it could also be creating some kind of memory leak.请注意,对于阅读您的代码的人来说,有很多未知数,例如df = x.get_ohlc_data() ,我不知道那在做什么,而且它也可能造成某种 memory 泄漏。 Best to reduce your problem to something others reading your post can run on their end and observe the memory leak...最好将您的问题减少到其他人阅读您的帖子可以运行并观察 memory 泄漏...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM