Python 模块tracemalloc提供了对程序分配内存的详细了解。例如,一个用例是记录当前和峰值内存使用情况:
import tracemalloc
tracemalloc.start()
# ...code...
current, peak = tracemalloc.get_traced_memory()
如果我们现在想要重置峰值,文档建议使用tracemalloc.reset_peak()
. 但是,这个功能只是在 Python 3.9 中添加的,我想知道我是否可以达到相同的效果tracemalloc.clear_traces()
?
我的用例是这样的:
for i in range(10):
# do sth
current, peak = tracemalloc.get_traced_memory()
print('Current and peak memory usage: {} {}'.format(current, peak))
# clear peak memory
所以对于i
for循环中的每一个,我都会做某事并且只想测量我创建的内存。峰值应该只针对每个索引,而不是针对全局运行,这就是我要清除它的原因。
编辑:为了测试和之间的区别reset_peak()
,clear_traces()
我测试了这个程序:
tracemalloc.start()
current_memories = []
peak_memories = []
for i in range(10):
a = list(range(100000))
current, peak = tracemalloc.get_traced_memory()
current_memories.append(current/(1024*1024))
peak_memories.append(peak/(1024*1024))
tracemalloc.reset_peak()
# tracemalloc.clear_traces()
del current, peak
print('Average current memory [MB]: {}, average peak memory [MB]: {} +/- {}'.format(
round(np.mean(current_memories), 4), round(np.mean(peak_memories), 4),
round(np.std(peak_memories), 4))
)
当我测试clear_traces()
时,输出是:
Average current memory [MB]: 3.4273, average peak memory [MB]: 3.4274 +/- 0.0019
当我改为使用reset_peak()
时,我获得:
Average current memory [MB]: 3.4313, average peak memory [MB]: 6.5156 +/- 1.0273
为什么这两种方法在显示的内存量上存在差异?