-
-
Save zodman/6695998 to your computer and use it in GitHub Desktop.
memory usage for a process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import psutil | |
PROCESS = psutil.Process(os.getpid()) | |
MEGA = 10 ** 6 | |
MEGA_STR = ' ' * MEGA | |
def pmem(): | |
tot, avail, percent, used, free,_,_,_,_ = psutil.virtual_memory() | |
tot, avail, used, free = tot / MEGA, avail / MEGA, used / MEGA, free / MEGA | |
proc = PROCESS.get_memory_info()[1] / MEGA | |
print('process = %s total = %s avail = %s used = %s free = %s percent = %s' | |
% (proc, tot, avail, used, free, percent)) | |
def alloc_max_array(): | |
i = 0 | |
ar = [] | |
while True: | |
try: | |
#ar.append(MEGA_STR) # no copy if reusing the same string! | |
ar.append(MEGA_STR + str(i)) | |
pmem() | |
except MemoryError: | |
break | |
i += 1 | |
max_i = i - 1 | |
print 'maximum array allocation:', max_i | |
pmem() | |
def alloc_max_str(): | |
i = 0 | |
while True: | |
pmem() | |
try: | |
a = ' ' * (i * 10 * MEGA) | |
del a | |
except MemoryError: | |
break | |
i += 1 | |
max_i = i - 1 | |
_ = ' ' * (max_i * 10 * MEGA) | |
print 'maximum string allocation', max_i | |
pmem() | |
pmem() | |
alloc_max_str() | |
alloc_max_array() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment