memorymonitor – 内存监控助手¶
- 
exception memorymonitor.AllocationError¶
- 基地: - Exception- 分配相关错误的 Catchall 异常。 - 初始化自我。请参阅 help(type(self)) 以获得准确的签名。 
- 
class memorymonitor.AllocationAlarm(*, minimum_block_count: int = 1)¶
- 分配一个 minimum_block_count或多个块时抛出异常
- 活动时发生。 
 - 跟踪分配: - import memorymonitor aa = memorymonitor.AllocationAlarm(minimum_block_count=2) x = 2 # Should not allocate any blocks. with aa: x = 5 # Should throw an exception when allocating storage for the 20 bytes. with aa: x = bytearray(20) - 
ignore(self, count: int) → AllocationAlarm¶
- 在引发异常之前设置要忽略的适用分配的数量。在上下文退出时自动设置回零。 - 在 - with块中使用它:- # Will not alarm because the bytearray allocation will be ignored. with aa.ignore(2): x = bytearray(20) 
 - 
__enter__(self) → AllocationAlarm¶
- 启用警报。 
 - 
__exit__(self) → None¶
- 退出上下文时自动禁用分配警报。有关更多信息,请参阅 Lifetime 和 ContextManagers 。 
 
- 分配一个 
- 
class memorymonitor.AllocationSize¶
- 跟踪两个桶的幂的分配数量。 - 它将有 16 个 16 位桶来跟踪分配计数。它是总分配意味着忽略释放。重新分配的内存在分配时和重新分配时计算两次。 - 桶是根据块来衡量的,块是堆的最细粒度。这意味着桶 0 将计算小于或等于每个块的字节数的所有分配,通常为 16。桶 2 将小于或等于 4 个块。请参阅 - bytes_per_block将块转换为字节。- 多个 AllocationSize 可用于跟踪不同的代码边界。 - 跟踪分配: - import memorymonitor mm = memorymonitor.AllocationSize() with mm: print("hello world" * 3) for bucket, count in enumerate(mm): print("<", 2 ** bucket, count) - 
bytes_per_block:int¶
- 每个块的字节数 
 - 
__enter__(self) → AllocationSize¶
- 清除计数并恢复跟踪。 
 - 
__exit__(self) → None¶
- 退出上下文时自动暂停分配跟踪。有关更多信息,请参阅 Lifetime 和 ContextManagers 。 
 
-