Matplotlib:内存和“CPU"泄漏 [英] Matplotlib: Memory and 'CPU' leak

查看:58
本文介绍了Matplotlib:内存和“CPU"泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python: 2.7
Ubuntu: 18.04
matpltolib: 2.2.2

我有一个客户端 GUI,可以从服务器获取信息并显示它.我看到内存泄漏和 CPU 消耗随时间的变化.下图显示了使用 GUI 重新启动客户端后 CPU 和内存利用率的变化(从右侧开始约 25 秒,与网络流量中的香料对齐).

I have a client GUI that get information from a server and displays it. I see memory leak and change in CPU consumption with time. The picture below shows a change in CPU and memory utilization after restarting the client with GUI (~25 seconds from the right, aligned with a spice in network traffic).

CPU 使用率图有一个下降,表明程序重启前后 CPU 使用率不同.

The CPU graph has a dip in the CPU utilization showing that CPU usage is different before and after the restart of program.

内存图显示内存利用率大幅下降,然后由于同一程序的初始化而略有增加.

The Memory graph shows a large drop in the memory utilization and then slight increase due to initialization of the same program.

网络图有一个尖峰,因为客户端从服务器请求所有数据以进行可视化.

The Network graph has a spike because the client requests all data from the server for visualization.

我怀疑这与 matplotlib 有关.我有 7 个数字,我每 3 秒重新绘制一次.

I suspect it is something to do with matplotlib. I have 7 figures that I rechart every 3 seconds.

我已经添加了我的 GUI 图像.中间的 4 个图是历史图表.但是,我将所有数据点分为 300 个 bin,因为该区域有 ~ 300 个像素.分箱是在一个单独的线程中完成的.存储信息的数据数组(2x1 000 000 个点、时间和值)是从一开始就创建的,以确保我的数据集增长时不会出现任何内存失控问题.我不认为数据集会超过这个范围,因为典型的实验以 0.1-0.01 Hz 的频率运行,这需要几百万秒才能结束.

I have added the image of my GUI. The middle 4 graphs are the history charts. However, I am binning all data points in 300 bins since I have ~ 300 pixels in that area. The binning is done in a separate thread. The data arrays( 2x1 000 000 points, time and value) that store the information are created from the very beginning to ensure that I don't have any memory runaway problem when my datasets grow. I do not expect the datasets to grow beyond that since the typical experiment runs at 0.1-0.01 Hz which will take several million seconds to reach the end.

问题:如果是Matplotlib,怎么办?如果不是,那还能是什么?

Question: If it is Matplotlib, what can I do? If it is not, what else could it be?

于 2018 年 9 月 6 日添加:我想再添加一个例子.这是我关闭 GUI 后 CPU 和内存使用情况的屏幕截图.代码运行了大约 3 天.Python 2.7,Ubuntu 18.04.1.

added Sept 6 2018: I thought of adding another example. Here is the screenshot of CPU and memory usage after I closed the GUI. The code ran for ~ 3 days. Python 2.7, Ubuntu 18.04.1.

推荐答案

感谢大家提出有用的意见.经过一番挣扎,我找到了解决问题的方法.不幸的是,我对我的代码做了几处更改,所以我不能确切地说是什么真正有帮助.

Thank you, everyone, for helpful comments. After some struggle, I have figure out the way to solve the problem. Unfortunately, I have made several changes in my code, so I cannot say definitively what actually helped.

这里做了什么:

  1. 所有图表都是在一个单独的线程中完成的.图像通过 io.Bytes() 作为字节流保存在缓冲区中,然后传递给 GUI.这对我解决另一个问题很重要(使用 matplotlib 绘制图表时 GUI 冻结).

每次生成绘图时创建一个新图形(figure = Figure(figsize=(7,8),dpi=80)).以前我一直在重复使用相同的图(self.figure = Figure(figsize=(7,8),dpi=80)).

create a new figure(figure = Figure(figsize=(7,8),dpi=80)) each time you generate the plot. previously I have been reusing the same figure (self.figure = Figure(figsize=(7,8),dpi=80)).

这篇关于Matplotlib:内存和“CPU"泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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