【发布时间】:2021-12-29 22:25:00
【问题描述】:
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
所以对于for循环中的每个i,我都会做某事并且只想测量我创建的内存。峰值应该只针对每个索引,而不是针对全局运行,这就是我要清除它的原因。
编辑:为了测试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
为什么这两种方法在显示的内存量上存在差异?
【问题讨论】:
-
你为什么要问我们?你有没有测试过它自己是否有效?
-
所以,你只需要记住你得到的最后一个值,然后从新值中减去它。你不需要清除任何东西。
-
@TimRoberts 不确定我能不能跟上。毕竟,如果我做了两件事,第二个操作的内存消耗会比第一个操作少很多,那么
peak仍然会有第一个操作的内存消耗,对吧?
标签: python tracemalloc