需要帮助确定涉及matplotlib和烧瓶的内存泄漏 [英] Need help identifying a memory leak involving matplotlib and flask

查看:201
本文介绍了需要帮助确定涉及matplotlib和烧瓶的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个小型的Web应用程序使用的烧瓶框架,涉及使用matplotlib绘图。问题是我每次创建plot时,进程都会消耗更多的内存。

我已经使用带有.wsgi文件的mod_wsgi部署应用程序,看起来就像这样: / p>

 从yourapplication导入应用程序作为应用程序

当我访问创建图的url时,问题就开始了。该函数创建一个绘图仪对象,当它初始化时,从sqlite3数据库(数据由大约30个整数和同样多的日期时间对象组成)获取相关数据,使用matplotlib创建一个绘图并返回一个StringIO对象,然后显示在屏幕上。

这是函数的结尾。全班可以看这里

  canvas = FigureCanvas(fig)
png_output = StringIO.StringIO()
canvas.print_png(png_output)
return png_output。 getvalue()

当我访问该站点时,会创建一个具有大约25MB保留内存的进程。我第一次创建一个阴谋,它增长到30MB,然后每次使用绘图仪类大约1MB。默认设置是使用5个进程消耗太多的内存(在几分钟内达到150MB,我只允许80MB)。

我对这里所涉及的所有东西(web框架,apache,数据库)都很陌生,所以我没有任何感觉可能会出错。任何想法,高度赞赏。谢谢!

解决方案在每次调用 plot_month 函数之后,

  import gc 
gc.collect()


I have written a small webapp using the flask framework that involves plotting using matplotlib. The problem is that every time I create the plot, the process consumes more memory.

I have deployed the app using mod_wsgi with a .wsgi file looking simply like this:

from yourapplication import app as application

The problems start when I access the url which creates the plot. The function creates a plotter object which, when initilized, takes the relevant data from a sqlite3 database (the data consist of about 30 integers and equally many datetime objects), creates a plot using matplotlib and returning a StringIO object which then is displayed on screen.

This is the end of the function. The whole class can be seen here

    canvas = FigureCanvas(fig)
    png_output = StringIO.StringIO()
    canvas.print_png(png_output)
    return png_output.getvalue()

When I visit the site, a process is created with about 25MB of reserved memory. The first time I create a plot it grows to 30MB and then with about 1MB for each time I use the plotter class. The default settings were using 5 process which consumed way too much memory (was up to 150MB within minutes and I'm only allowed 80MB).

I'm very new to all things involved here (web frameworks, apache, databases) so I don't have any feeling of were things might be going wrong, so any ideas are highly appreciated. Thanks!

解决方案

Doing this after each call to the plot_month function solved the leak.

import gc
gc.collect()

这篇关于需要帮助确定涉及matplotlib和烧瓶的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆