Skip to main content

IPython magic commands for tracking memory usage

Project description

IPython memory magic commands

PyPI

Simple tool to trace memory usage of a Python statement or expression execution.

Memory equivalent of IPython built-in time magics.

Existing tools like memory-profiler or ipython-memory-usage mainly use psutil package to measure memory usage, which may give inaccurate results. This package uses tracemalloc module to trace Python memory allocations. Memory-profiler provides tracemalloc backend, but it does not allow to use it for magic commands. This packages offers line %memory and cell %%memory magic commands, which were intended to complement the %time and %%time magic commands.

Installation

Install from pip:

pip install ipython-memory-magics

Or install directly from github:

pip install git+https://github.com/rusmux/ipython-memory-magics.git

After the installation load the extension via:

%load_ext memory_magics

To activate it whenever you start IPython, edit the configuration file for your IPython profile ~/.ipython/profile_default/ipython_config.py. Register the extension like this:

c.InteractiveShellApp.extensions = [
    'memory_magics',
]

If the file does not already exist, run ipython profile create in a terminal.

Usage

Use %memory [options] statement to measure statement's memory consumption:

%memory -q list(range(10**6))

The output in the format current / peak will follow:

RAM usage: line: 34.33 MiB / 34.33 MiB

Here -q is the quiet flag set to suppress the output. You can use other options to get data on the notebook and jupyter memory usage, or to print the statistics in a table. For example, you can use -n or --notebook flag to get the information about the notebook current memory consumption:

%memory -n
RAM usage: notebook: 101.41 MiB

In the same way, -j or --jupyter flag will give you the information about the total Jupyter memory usage.

Put %%memory [options] on top of a cell to measure its memory consumption:

In [1]: %%memory -n -j -t
        sum(list(range(10**6)))

This will print:

RAM usage |   current   |     peak     |
----------------------------------------
 cell     | 2.62 KiB    | 34.33 MiB    |
 notebook | 123.08 MiB  | 155.53 MiB   |
 jupyter  | 170.19 MiB  | 202.55 MiB   |

Out [1]: 499999500000

Options

Five options are available in full and short versions:

-n <notebook>: If present, show current notebook memory usage

-j <jupyter>: If present, show current jupyter memory usage

-i <interval>: Interval in milliseconds for updating memory usage information

-t <table>: If present, print statistics in a table

-q <quiet>: If present, do not return the output

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ipython-memory-magics-0.3.7.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

ipython_memory_magics-0.3.7-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file ipython-memory-magics-0.3.7.tar.gz.

File metadata

  • Download URL: ipython-memory-magics-0.3.7.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.6 Darwin/21.6.0

File hashes

Hashes for ipython-memory-magics-0.3.7.tar.gz
Algorithm Hash digest
SHA256 8f0957953e050fc013820c3e6a6a3dbdeadd3723bd7740000d0c103a1203a440
MD5 56d8c56e6e60513ca53e21838fd52957
BLAKE2b-256 798737eae43fa0548d89e051a23ac4c26829e58b05f578e9fff868054b8d1827

See more details on using hashes here.

File details

Details for the file ipython_memory_magics-0.3.7-py3-none-any.whl.

File metadata

File hashes

Hashes for ipython_memory_magics-0.3.7-py3-none-any.whl
Algorithm Hash digest
SHA256 26eca9485c806f8acef41ef7d3728e37c903839645ea8cc61ce9f8a47405ee97
MD5 41dcd414490383605435303e1847d92e
BLAKE2b-256 db0b9273802f3eff10546d97aa26c87c4154f5d12fea62702ffb1d040490ad8e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page