-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_logging.py
40 lines (37 loc) · 1.25 KB
/
memory_logging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Enable memory logging by setting builtins.memory_logging_enabled = True before importing this the first time.
# import builtins
# builtins.memory_logging_enabled = True
try:
global memory_logging_enabled
if memory_logging_enabled:
print('Enabling memory instrumentation')
except NameError:
# Set False by default to remove runtime overhead of decorators and make everything else early-out
memory_logging_enabled = False
if memory_logging_enabled:
import gc
import time
baseline = gc.mem_alloc()
last_invocation = time.monotonic_ns()
# @instrumentation.metrics.timer('print_mem')
def print_mem(when: str):
global baseline
global last_invocation
alloc_start = gc.mem_alloc()
gc.collect()
alloc_after = gc.mem_alloc()
garbage = alloc_start - alloc_after
now = time.monotonic_ns()
print(' {:>8.1f}ms {:36s}: free:{:6d} alloc:{:6d} difference:{:6d} garbage:{:6d}'.format(
(now - last_invocation) / 1000000,
when,
gc.mem_free(),
alloc_after,
alloc_after-baseline,
garbage
))
baseline = alloc_after
last_invocation = now
else:
def print_mem(when):
pass