我想在生产系统中记录Python脚本的当前内存使用情况。AWS有Container Insights,但它们隐藏得非常好,我不知道如何在其他仪表板/日志记录和修改系统中正确使用它们。另外,我也不确定日志峰值内存是否存在
Python脚本是生产系统。它在Docker容器中的AWS上运行,我遇到了以前的方法(link)的问题
tracemalloc似乎能给我想要的信息:
# At the start of the script
import tracemalloc
tracemalloc.start()
# script running...
# At the end
current, peak = tracemalloc.get_traced_memory()
logger.info(f"Current memory usage is {current / 10**6} MB")
logger.info(f"Peak memory usage was {peak / 10**6} MB")
tracemalloc.stop()
但是,该文件规定:
The tracemalloc module is a debug tool
那么,围绕生产代码进行包装是不是一个坏主意?费用是多少?有没有其他理由不在生产中使用
(我非常清楚代码的哪些部分需要最多内存,以及达到内存峰值的位置。我想监控该部分(或者更确切地说,监控那些少数对象/几行代码的大小)。tracemalloc的替代方案似乎是使用一些like this)
我一直在试图回答同样的问题。我找到的最好的答案是
https://www.mail-archive.com/python-list@python.org/msg443129.html
根据一个简单的实验,它引用了一个3-4倍的因子来增加tracemalloc的内存使用
相关问题 更多 >
编程相关推荐