Matplotlib 错误会导致内存泄漏。我怎样才能释放该内存?

发布于 2024-11-30 09:06:30 字数 748 浏览 0 评论 0原文

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

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

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

谢谢。

这是一些给我带来聚合复杂度错误的代码。

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()

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'

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.

Does anyone know how I can release that memory?

Thanks.

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

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趴在窗边数星星i 2024-12-07 09:06:30

我在这里找到
http://www.mail-archive.com/<跨度类=“__cf_email__” data-cfemail="3855594c4854574c54515a154d4b5d4a4b7854514b4c4b164b574d4a5b5d5e574a5f5d16565d4c">[电子邮件受保护]/msg11809.html
,它给出了一个有趣的答案,可能有助于

尝试将 : 替换

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

from matplotlib import figure
fig = figure.Figure()

I find here
http://www.mail-archive.com/[email protected]/msg11809.html
, it gives an interesting answer that may help

try replacing :

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

with

from matplotlib import figure
fig = figure.Figure()
肥爪爪 2024-12-07 09:06:30

我假设您可以运行您发布的代码至少一次。该问题仅在多次运行发布的代码后才会显现出来。正确的?

如果是这样,下面的方法就可以避免该问题,而无需真正确定问题的根源。
也许这是一件坏事,但这在紧要关头有效:只需使用多处理在单独的进程中运行内存密集型代码即可。您不必担心 fig.clf()plt.close()del a,bgc .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()

您也不必proc.join()join 将阻塞主进程,直到 worker 完成。如果省略join,则主进程将继续,worker 进程在后台运行。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文