Skip to content

Instantly share code, notes, and snippets.

@amarao
Created June 4, 2018 09:45
Show Gist options
  • Save amarao/893894ff9d0d9546601eda4af7e058dd to your computer and use it in GitHub Desktop.
Save amarao/893894ff9d0d9546601eda4af7e058dd to your computer and use it in GitHub Desktop.
Illustration for python memory fragmentation issue
#!/usr/bin/python3
import random
import time
MAX_HOG = 1000*1000
def sparse(something):
for element in something:
victim = element["big"]
element["big"] = None
del victim
def hog():
res=[]
for counter in range(0,MAX_HOG):
res.append({
"small": random.random(),
"big": "%3500f" % random.random()
})
return res
def print_mem_footprint(something):
sum_all = 0
dicts = 0
big = 0
small = 0
for element in something:
dicts += element.__sizeof__()
big += element["big"].__sizeof__()
small += element["small"].__sizeof__()
sum_all = something.__sizeof__() + dicts + big + small
print("List size: %s" % something.__sizeof__())
print("Total dicts size: %s" % dicts)
print("Total small objects size: %s" % small)
print("Total big objects size: %s" % big)
print("Sum of above: %s" % sum_all)
if __name__ == '__main__':
res=hog()
print("Memory after hogging")
print_mem_footprint(res)
sparse(res)
print ("-" * 60)
print("Memory after sparsing")
print_mem_footprint(res)
print("Press Ctrl-C to exit")
while True:
time.sleep(3600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment