tracemalloc的开销有多大?

2024-10-05 13:16:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在生产系统中记录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


Tags: the内存代码脚本aws系统记录script

热门问题