You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python tracemalloc测量内存分配:reset_peak()与clear_traces()的差异原因问询

Understanding the Difference Between tracemalloc.reset_peak() and tracemalloc.clear_traces()

First off, let's cut to the chase: you can't use clear_traces() as a drop-in replacement for reset_peak()—they do fundamentally different things, which explains the huge variance in your test results. Let's break this down.

What Each Function Actually Does

Let's start with the core purpose of each method:

  • tracemalloc.reset_peak(): This is a targeted function designed only to reset the peak memory counter. It leaves all existing memory allocation traces intact, so tracemalloc keeps tracking the objects it already knows about. The peak gets set to the current memory usage, meaning the next peak measurement will only reflect memory changes from that point onward.
  • tracemalloc.clear_traces(): This wipes the slate completely. It deletes all recorded allocation and release traces, effectively resetting the entire tracemalloc tracking state. Not only does this reset the peak, but it also makes tracemalloc lose track of all existing objects—so the next memory measurement will only account for objects allocated after the clear.

Why Your Test Results Are So Different

Looking at your test code, let's walk through what's happening in each scenario:

When using reset_peak()

Each iteration creates a large list a. Since you don't explicitly delete a between iterations, Python's garbage collector might not immediately clean up the old list when you reassign a in the next loop. This means:

  • In some iterations, you might have two large lists in memory at once (the old one waiting to be GC'd and the new one you just created).
  • reset_peak() only resets the peak counter, not the tracking context—so tracemalloc still sees all active objects. The peak ends up reflecting the combined memory of both lists in those cases, leading to higher average peak values and more variance (hence the 6.5MB average and 1.0273 std dev).

When using clear_traces()

Every time you call clear_traces(), you're erasing all past allocation records. This means tracemalloc starts fresh each iteration, so it only tracks the new list a you create. The old list (even if it's still in memory waiting for GC) isn't counted because tracemalloc no longer has traces of its allocation. That's why your current and peak values are nearly identical (~3.4MB) with minimal variance—you're only measuring the memory of the single list created in that iteration.

How to Properly Measure Per-Iteration Peak Memory

Your goal is to measure peak memory only for each individual iteration. Here's how to do it right:

For Python 3.9+ (use the official solution)

Stick with tracemalloc.reset_peak()—it's exactly what this function was built for. To make your test more reliable, explicitly delete the large object after measuring to help the GC clean up immediately:

import tracemalloc
import numpy as np

tracemalloc.start()
current_memories = []
peak_memories = []
for i in range(10):
    tracemalloc.reset_peak()  # Reset peak to current memory at start of iteration
    a = list(range(100000))
    current, peak = tracemalloc.get_traced_memory()
    current_memories.append(current/(1024*1024))
    peak_memories.append(peak/(1024*1024))
    del a  # Explicitly delete the object to free memory

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))
)

For Python 3.8 and below (no reset_peak())

If you need compatibility with older Python versions, you can restart tracemalloc each iteration to get a clean state:

import tracemalloc
import numpy as np

current_memories = []
peak_memories = []
for i in range(10):
    # Restart tracing to get a fresh state
    tracemalloc.stop()
    tracemalloc.clear_traces()
    tracemalloc.start()
    
    a = list(range(100000))
    current, peak = tracemalloc.get_traced_memory()
    current_memories.append(current/(1024*1024))
    peak_memories.append(peak/(1024*1024))
    del a

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))
)

This will give you results similar to the reset_peak() approach, as each iteration starts with a fully reset tracking state.

内容的提问来源于stack exchange,提问作者Imahn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:47:42