import tracemalloc
import gc
tracemalloc.start(1)
y = None
def dump_tracemalloc_snapshot():
gc.collect()
snapshot = tracemalloc.take_snapshot()
stats = snapshot.statistics('traceback')
for stat in stats[:3]:
print("%s memory blocks: %.1f KiB" % (stat.count, stat.size / 1024))
for line in stat.traceback.format():
print(line)
print('\n\n')
def allocate_memory():
global y
x = []
for i in range(100000):
x.append(f"AAA{i}")
y = x
if __name__=="__main__":
allocate_memory()
dump_tracemalloc_snapshot()
del y
dump_tracemalloc_snapshot()
1条答案
按热度按时间dgtucam11#
根据我所做的测试,
tracemalloc
似乎不跟踪释放的内存。输出: