Matplotlib错误导致内存泄漏。我怎样才能释放那段记忆呢?

时间:2021-02-27 21:17:20

I am running a django app that includes matplotlib and allows the user to specify the axes of the graph. This can result in 'Overflow Error: Agg complexity exceeded'

我正在运行一个django应用程序,它包含matplotlib,允许用户指定图形的轴。这会导致“溢出错误:超出Agg复杂性”

When that happens up to 100MB of RAM get tied up. Normally I free that memory up using fig.gcf(), plot.close(), and gc.collect(), but the memory associated with the error does not seem to be associated with the plot object.

当这种情况发生时,100MB内存就会被占用。通常,我使用fig.gcf()、plot.close()和gc.collect()释放内存,但是与错误相关的内存似乎与plot对象没有关联。

Does anyone know how I can release that memory?

有人知道我怎样才能释放那段记忆吗?

Thanks.

谢谢。

Here is some code that gives me the Agg Complexity Error.

下面是一些代码,它给出了Agg复杂性错误。

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np      
import gc

a = np.arange(1000000)
b = np.random.randn(1000000)

fig = plt.figure(num=1, dpi=100, facecolor='w', edgecolor='w')
fig.set_size_inches(10,7)
ax = fig.add_subplot(111)
ax.plot(a, b)

fig.savefig('yourdesktop/random.png')   # code gives me an error here

fig.clf()    # normally I use these lines to release the memory
plt.close()
del a, b
gc.collect()

2 个解决方案

#1


9  

I assume you can run the code you posted at least once. The problem only manifests itself after running the posted code many times. Correct?

我认为您至少可以运行一次发布的代码。问题只有在多次运行已发布的代码之后才会显现出来。正确吗?

If so, the following avoids the problem without really identifying the source of the problem. Maybe that is a bad thing, but this works in a pinch: Simply use multiprocessing to run the memory-intensive code in a separate process. You don't have to worry about fig.clf() or plt.close() or del a,b or gc.collect(). All memory is freed when the process ends.

如果是这样,下面的代码避免了问题,而没有真正识别问题的根源。这可能是一件坏事,但这在紧要关头起作用:只需在单独的进程中使用多处理来运行内存密集型的代码。您不必担心fig.clf()或pl .close()或del a、b或gc.collect()。进程结束时释放所有内存。

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np      

import multiprocessing as mp

def worker():
    N=1000000
    a = np.arange(N)
    b = np.random.randn(N)

    fig = plt.figure(num=1, dpi=100, facecolor='w', edgecolor='w')
    fig.set_size_inches(10,7)
    ax = fig.add_subplot(111)
    ax.plot(a, b)

    fig.savefig('/tmp/random.png')   # code gives me an error here

if __name__=='__main__':
    proc=mp.Process(target=worker)
    proc.daemon=True
    proc.start()
    proc.join()

You don't have to proc.join() either. The join will block the main process until the worker completes. If you omit the join, then the main process simply continues with the worker process working in the background.

也不需要proc.join()。连接将阻塞主进程,直到worker完成。如果您省略了连接,那么主进程将继续在后台工作。

#2


11  

I find here http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg11809.html , it gives an interesting answer that may help

我在这里找到http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg11809.html,它给出了一个有趣的答案可能会有所帮助

try replacing :

试着更换:

import matplotlib.pyplot as plt
fig = plt.figure()

with

from matplotlib import figure
fig = figure.Figure()

#1


9  

I assume you can run the code you posted at least once. The problem only manifests itself after running the posted code many times. Correct?

我认为您至少可以运行一次发布的代码。问题只有在多次运行已发布的代码之后才会显现出来。正确吗?

If so, the following avoids the problem without really identifying the source of the problem. Maybe that is a bad thing, but this works in a pinch: Simply use multiprocessing to run the memory-intensive code in a separate process. You don't have to worry about fig.clf() or plt.close() or del a,b or gc.collect(). All memory is freed when the process ends.

如果是这样,下面的代码避免了问题,而没有真正识别问题的根源。这可能是一件坏事,但这在紧要关头起作用:只需在单独的进程中使用多处理来运行内存密集型的代码。您不必担心fig.clf()或pl .close()或del a、b或gc.collect()。进程结束时释放所有内存。

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np      

import multiprocessing as mp

def worker():
    N=1000000
    a = np.arange(N)
    b = np.random.randn(N)

    fig = plt.figure(num=1, dpi=100, facecolor='w', edgecolor='w')
    fig.set_size_inches(10,7)
    ax = fig.add_subplot(111)
    ax.plot(a, b)

    fig.savefig('/tmp/random.png')   # code gives me an error here

if __name__=='__main__':
    proc=mp.Process(target=worker)
    proc.daemon=True
    proc.start()
    proc.join()

You don't have to proc.join() either. The join will block the main process until the worker completes. If you omit the join, then the main process simply continues with the worker process working in the background.

也不需要proc.join()。连接将阻塞主进程,直到worker完成。如果您省略了连接,那么主进程将继续在后台工作。

#2


11  

I find here http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg11809.html , it gives an interesting answer that may help

我在这里找到http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg11809.html,它给出了一个有趣的答案可能会有所帮助

try replacing :

试着更换:

import matplotlib.pyplot as plt
fig = plt.figure()

with

from matplotlib import figure
fig = figure.Figure()