Last active
December 26, 2015 05:09
-
-
Save valdergallo/7098249 to your computer and use it in GitHub Desktop.
Show how much you sistem can allocate memory
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 | |
import sys | |
""" | |
+---------------+-------------------------+-----------------------------+ | |
| Name | Number of Bytes | Amount of Text | | |
| Kilobyte (KB) | 2**10 or 1,024 | 1/2 page | | |
| Megabyte (MB) | 2**20 or 1,048,576 | 500 pages or 1 thick book | | |
+---------------+-------------------------+-----------------------------+ | |
font: http://www.wisegeek.org/how-much-text-is-in-a-kilobyte-or-megabyte.htm | |
""" | |
PROCESS = psutil.Process(os.getpid()) | |
MEGA = 2 ** 20 | |
MEGA_STR = ' ' * MEGA | |
def pmem(): | |
virtual_memory = psutil.virtual_memory() | |
tot = virtual_memory.total | |
avail = virtual_memory.available | |
percent = virtual_memory.percent | |
used = virtual_memory.used | |
free = virtual_memory.free | |
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) | |
except MemoryError: | |
break | |
i += 1 | |
max_i = i - 1 | |
print 'maximum array index: %s | maximum memory: %sMG' % (max_i, (sys.getsizeof(ar)/MEGA)) | |
pmem() | |
def alloc_max_str(): | |
i = 0 | |
while True: | |
try: | |
a = ' ' * (i * MEGA) | |
del a | |
except MemoryError: | |
break | |
i += 1 | |
max_i = i - 1 | |
_ = ' ' * (max_i * MEGA) | |
print 'maximum string size: %s | maximum memory: %sMG' % (max_i, (sys.getsizeof(_)/MEGA)) | |
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