Skip to content

Instantly share code, notes, and snippets.

@mjpieters
Created June 26, 2018 14:28
Show Gist options
  • Save mjpieters/1d9ce2c84b858ef7cb7192311e49bb49 to your computer and use it in GitHub Desktop.
Save mjpieters/1d9ce2c84b858ef7cb7192311e49bb49 to your computer and use it in GitHub Desktop.
# Python 2.7.13 build with pytracemalloc patches applied.
$ python27_tracemalloc test-pickle-tracemalloc.py
Python version info: 2.7.13 (default, Jun 25 2018, 22:08:02)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
Baseline (Tot: 0.0 MiB, Inc: 0.0 MiB)
Data created (Tot: 200.0 MiB, Inc: 200.0 MiB)
Pickle dumped (Tot: 246.0 MiB, Inc: 46.0 MiB)
$ python27_tracemalloc test-pickle-tracemalloc.py load
Python version info: 2.7.13 (default, Jun 25 2018, 22:08:02)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
Baseline (Tot: 0.0 MiB, Inc: 0.0 MiB)
Pickle loaded (Tot: 199.0 MiB, Inc: 199.0 MiB)
$ python3.6 test-pickle-tracemalloc.py
Python version info: 3.6.5 (default, May 7 2018, 16:43:32)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)]
Baseline (Tot: 0.0 MiB, Inc: 0.0 MiB)
Data created (Tot: 200.0 MiB, Inc: 200.0 MiB)
Pickle dumped (Tot: 246.0 MiB, Inc: 46.0 MiB)
$ python3.6 test-pickle-tracemalloc.py load
Python version info: 3.6.5 (default, May 7 2018, 16:43:32)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)]
Baseline (Tot: 0.0 MiB, Inc: 0.0 MiB)
Pickle loaded (Tot: 199.6 MiB, Inc: 199.6 MiB)
from __future__ import print_function
import tracemalloc
import pickle
import random
from string import ascii_uppercase
try:
# Python 2 compatibility
range = xrange
try:
# use the optimised version, in Py2 this requires an explicit import
import cPickle as pickle
except ImportError:
pass
except NameError:
pass
last = None
def display_memory_change(msg):
global last
snap = tracemalloc.take_snapshot()
statdiff, last = snap.compare_to(last, 'filename', True), snap
tot = sum(s.size for s in statdiff)
change = sum(s.size_diff for s in statdiff)
print('{:>20} (Tot: {:6.1f} MiB, Inc: {:6.1f} MiB)'.format(
msg, tot / 2 ** 20, change / 2 ** 20))
def random_string(_c=random.choice, _l=ascii_uppercase):
return "".join([_c(_l) for _ in range(20)])
def create_file():
# use locals for faster looping
rfloat, rstring, rrange = random.random, random_string, random.randrange
x = [(rfloat(), rstring(), rrange(2 ** 64)) for _ in range(1000000)]
display_memory_change('Data created')
pickle.dump(x, open('machin.pkl', 'wb'))
display_memory_change('Pickle dumped')
def load_file():
y = pickle.load(open('machin.pkl', 'rb'))
display_memory_change('Pickle loaded')
return y
if __name__=="__main__":
import sys
print('Python version info:', sys.version)
tracemalloc.start()
last = tracemalloc.take_snapshot()
display_memory_change('Baseline')
if 'load' not in sys.argv:
create_file()
else:
load_file()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment