Matplotlib 错误会导致内存泄漏。我怎样才能释放该内存?
我正在运行一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在这里找到
http://www.mail-archive.com/<跨度类=“__cf_email__” data-cfemail="3855594c4854574c54515a154d4b5d4a4b7854514b4c4b164b574d4a5b5d5e574a5f5d16565d4c">[电子邮件受保护]/msg11809.html
,它给出了一个有趣的答案,可能有助于
尝试将 : 替换
为
I find here
http://www.mail-archive.com/[email protected]/msg11809.html
, it gives an interesting answer that may help
try replacing :
with
我假设您可以运行您发布的代码至少一次。该问题仅在多次运行发布的代码后才会显现出来。正确的?
如果是这样,下面的方法就可以避免该问题,而无需真正确定问题的根源。
也许这是一件坏事,但这在紧要关头有效:只需使用多处理在单独的进程中运行内存密集型代码即可。您不必担心
fig.clf()
或plt.close()
或del a,b
或gc .collect()。当进程结束时,所有内存都将被释放。
您也不必
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 aboutfig.clf()
orplt.close()
ordel a,b
orgc.collect()
. All memory is freed when the process ends.You don't have to
proc.join()
either. Thejoin
will block the main process until theworker
completes. If you omit thejoin
, then the main process simply continues with theworker
process working in the background.