-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmu.py
29 lines (21 loc) · 1.05 KB
/
pmu.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
import sys
'''
PMU : Python 3 Memory Usage library
'''
def size_to_str(size):
if size <= 0:
return "ERROR : size <= 0"
for unit in [' ', 'Kio', 'Mio', 'Gio', 'Tio']:
if size < 1024.0:
return "%4.1f %s" % (size, unit)
size /= 1024.0
return ">= 1 Pio"
def view(min_size=1024, max_items=10):
print("╔══════════════════════╦═══════════════╗")
print("║ Name ║ Size ║")
print("╠══════════════════════╬═══════════════╣")
for name, size in sorted(((name, sys.getsizeof(value)) for name, value in globals().items()),
key=lambda x: -x[1])[:max_items]:
if size >= min_size:
print("║ {:<20} ║ {:>12} ║".format(name, size_to_str(size)))
print("╚══════════════════════╩═══════════════╝")