1

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 

所以对于ifor循环中的每一个,我都会做某事并且只想测量我创建的内存。峰值应该只针对每个索引,而不是针对全局运行,这就是我要清除它的原因。

编辑:为了测试和之间的区别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

为什么这两种方法在显示的内存量上存在差异?

4

1 回答 1

0

reset_peak在python3.9之前 似乎无法模仿。reset_peak是一个代码为 的C 函数peak = current,而内存计数器是C 变量,因此无法在python 中对其进行修改。

使用 clear_traces 会忘记所有先前的分配:

>>> import tracemalloc
>>> tracemalloc.start()
>>> a = list(range(1000))
>>> tracemalloc.get_traced_memory()
(37619, 47929)
>>> tracemalloc.clear_traces()
>>> tracemalloc.get_traced_memory()
(8691, 19001)
于 2021-12-29T23:39:02.627 回答