Measure (max) memory usage with IPython—like timeit but memit

In fact, it already exists, as part of the pragmatically named memory_profiler package:

In [2]: %memit np.zeros(1e7)
maximum of 3: 76.402344 MB per loop

More info at https://github.com/pythonprofilers/memory_profiler#ipython-integration

Edit: To use this, you first need to load it as an IPython extension:

%load_ext memory_profiler

To make IPython always load the memory_profiler extension upon startup, add it to the c.InteractiveShellApp.extensions list in your profile’s ipython_config.py:

$ grep -C2 c.InteractiveShellApp.extensions ~/.ipython/profile_default/ipython_config.py
 # A list of dotted module names of IPython extensions to load.
 #
 c.InteractiveShellApp.extensions = [
   'autoreload',
   'memory_profiler',

Leave a Comment

tech