memorymonitor

Memory monitoring helpers

exception memorymonitor.AllocationError

Bases: Exception

Catchall exception for allocation related errors.

class memorymonitor.AllocationAlarm(*, minimum_block_count: int = 1)
ignore(self, count: int)

Sets the number of applicable allocations to ignore before raising the exception. Automatically set back to zero at context exit.

Use it within a with block:

# Will not alarm because the bytearray allocation will be ignored.
with aa.ignore(2):
    x = bytearray(20)
__enter__(self)

Enables the alarm.

__exit__(self)

Automatically disables the allocation alarm when exiting a context. See Lifetime and ContextManagers for more info.

class memorymonitor.AllocationSize
bytes_per_block :int

Number of bytes per block

__enter__(self)

Clears counts and resumes tracking.

__exit__(self)

Automatically pauses allocation tracking when exiting a context. See Lifetime and ContextManagers for more info.

__len__(self)

Returns the number of allocation buckets.

This allows you to:

mm = memorymonitor.AllocationSize()
print(len(mm))
__getitem__(self, index: int)

Returns the allocation count for the given bucket.

This allows you to:

mm = memorymonitor.AllocationSize()
print(mm[0])