debug_malloc_stats Examples

These Python examples are in pymemtrace.examples.ex_debug_alloc_stats

Adding Small Strings

Here is an example of adding small strings to a list under the watchful eye of debug_malloc_stats.DiffSysDebugMallocStats:

print(f'example_debug_malloc_stats_for_documentation()')
with debug_malloc_stats.DiffSysDebugMallocStats() as malloc_diff:
    for i in range(1, 9):
        list_of_strings.append(' ' * (i * 8))
print(f'DiffSysDebugMallocStats.diff():')
print(f'{malloc_diff.diff()}')

The output is:

example_debug_malloc_stats_for_documentation()
DiffSysDebugMallocStats.diff():
class   size   num pools   blocks in use  avail blocks
-----   ----   ---------   -------------  ------------
    1     32          +1             +52           +74
    2     48          +0             +17           -17
    3     64          +0             +33           -33
    4     80          +1             +51            -1
    5     96          +2             +34           +50
    6    112          +0              +2            -2
    7    128          +0              +1            -1
   10    176          +0              +1            -1
   12    208          +0              +1            -1
   17    288          +0              +1            -1
   18    304          +0              +2            -2
   25    416          +0              +3            -3
   26    432          +0              +3            -3
   27    448          +0              +3            -3
   29    480          +0              +3            -3
   30    496          +0              +1            -1
   31    512          +0              +1            -1

# bytes in allocated blocks        =              +19,904
# bytes in available blocks        =               -3,808
-4 unused pools * 4096 bytes       =              -16,384
# bytes lost to pool headers       =                 +192
# bytes lost to quantization       =                  +96

  -1 free 1-sized PyTupleObjects * 32 bytes each =                  -32
  +1 free 5-sized PyTupleObjects * 64 bytes each =                  +64
           +2 free PyDictObjects * 48 bytes each =                  +96
           -2 free PyListObjects * 40 bytes each =                  -80
         +1 free PyMethodObjects * 48 bytes each =                  +48

Cost of debug_malloc_stats

In pymemtrace.examples.ex_debug_alloc_stats are some timing examples of creating a list of strings of varying size with and without debug_malloc_stats. Here are some typical results:

Times in µs

Task

Without debug_malloc_stats

With debug_malloc_stats

Ratio

128 byte strings

2.6

6400

x2400

1024 byte strings

5.3

6800

x1300

1Mb strings

590

7400

x12