Matplotlib: Memory and 'CPU' leak

核能气质少年 提交于 2020-01-06 05:25:10

问题


python: 2.7
Ubuntu: 18.04
matpltolib: 2.2.2

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

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.

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

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.

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

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.


回答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.

Here what was done:

  1. all charting is done in a separate thread. The image is saved in a buffer as a byte stream with io.Bytes() and later passed to the GUI. This was important for me to solve another problem(GUI freezes while charting with matplotlib).

  2. 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)).



来源:https://stackoverflow.com/questions/52040720/matplotlib-memory-and-cpu-leak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!