Last active
August 29, 2021 20:45
-
-
Save alexandru-dinu/a38976ca656b98ccc41aa4498483e19e to your computer and use it in GitHub Desktop.
Python over allocation
This file contains 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 sys | |
import psutil | |
UNIT = 2 ** 20 # MB | |
print("All size units are in M\n") | |
proc = psutil.Process(pid=os.getpid()) | |
m0 = proc.memory_info().rss | |
size = int(sys.argv[1]) * (2 ** 20) | |
print(f"Creating an int list `xs` of size {size // UNIT}.") | |
obj = [] | |
for i in range(size): | |
obj.append(i) | |
obj_size_M = sys.getsizeof(obj) / UNIT | |
print(f"\tgetsizeof(xs) = {obj_size_M:.3f}") | |
m1 = proc.memory_info().rss | |
print(f"\tresident mem difference (creating `xs`) = {(m1 - m0) / UNIT:.3f}") | |
print() | |
print("Copying `xs` to `ys`") | |
copy = obj[:] | |
copy_size_M = sys.getsizeof(copy) / UNIT | |
print(f"\tgetsizeof(ys) = {copy_size_M:.3f}") | |
m2 = proc.memory_info().rss | |
print(f"\tresident mem difference (creating `ys` copy) = {(m2 - m1) / UNIT:.3f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All size units are in M
list-3M
Creating an int list
xs
of size 3.Copying
xs
toys
Unpickling int list
xs
of size 3 (~15M on disk)list-128M
Creating an int list
xs
of size 128.Copying
xs
toys
Unpickling int list
xs
of size 128 (~641M on disk)numpy-128M
Creating an int32 numpy array
xs
of size 128.Copying
xs
toys
Unpickling int32 numpy array
xs
of size 128 (~513M on disk)