简体   繁体   English

使用 Python 和 matplotlib 在循环中绘图时如何避免过度使用 memory

[英]How to avoid excessive memory usage when plotting in a loop with Python and matplotlib

I have a Python program that gets data from a measurement instrument and plots the data using matplotlib (I am on Debian Linux).我有一个 Python 程序,它从测量仪器获取数据并使用 matplotlib 绘制数据(我在 Debian Linux 上)。 The plotting is done in a separate thread, which updates the data plots at fixed time intervals.绘图是在一个单独的线程中完成的,它以固定的时间间隔更新数据图。 At every update, the existing lines are removed from the plot, and then the lines are re-created with the new data (yes, there might be more efficient ways, but it's not possible to just add the new data to existing lines in my situation).每次更新时,从 plot 中删除现有行,然后使用新数据重新创建行(是的,可能有更有效的方法,但不可能只将新数据添加到我的现有行情况)。

After a while, the program will take up huge amounts of memory (gigabytes).一段时间后,该程序将占用大量的 memory(千兆字节)。 This does not happen if I modify the code to skip the plotting/matplotlib part, so the use of humungeous amounts of memory is clearly related to matplotlib.如果我修改代码以跳过绘图/matplotlib 部分,则不会发生这种情况,因此使用大量 memory 显然与 matplotlib 相关。 If I put some pressure on the system by running another application that will consume a lot of memory, my Python program will at some point start to release the excessive memory used up by matplotlib (ending up at about 50 MB or so), Releasing the memory does not seem to have any negative effects on the operation of my program. If I put some pressure on the system by running another application that will consume a lot of memory, my Python program will at some point start to release the excessive memory used up by matplotlib (ending up at about 50 MB or so), Releasing the memory 似乎对我的程序运行没有任何负面影响。 This tells me that the large junk of memory used by matplotlib is not vital (if not useless) in my application.这告诉我,matplotlib 使用的 memory 的大垃圾在我的应用程序中并不重要(如果不是无用的话)。

How can I avoid matplotlib from picking up so much memory?如何避免 matplotlib 捡到这么多 memory?

not sure if this will help, but have you tried using line-collections ?不确定这是否会有所帮助,但是您是否尝试过使用line-collections it should be more efficient for plotting a huge amount of lines in one go在一个 go 中绘制大量线条应该更有效

... aside of that some lines of code that show what you're doing might help to identify the problem ...除此之外,一些显示您正在做什么的代码行可能有助于识别问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM