Jupyter Integration#

We provide an IPython extension that adds a new Jupyter cell magic. This lets you create Memray flame graphs directly in Jupyter notebooks.

memray_flamegraph#

To load our IPython plugin, you simply need to run:

%load_ext memray

Once it’s loaded, you’ll have access to the %%memray_flamegraph cell magic. You can fill a Jupyter cell with %%memray_flamegraph on its own line, followed by some code whose memory usage you want to profile. Memray will run that cell’s code, tracking its memory allocations, and then display a flame graph directly in Jupyter for you to analyze.

It’s also possible to provide arguments on the %%memray_flamegraph line. For instance, %%memray_flamegraph --trace-python-allocators --leaks would let you look for memory not freed by the code in the cell:

%%memray_flamegraph --trace-python-allocators --leaks
def a():
    return "a" * 10_000

def bc():
    return "bc" * 10_000

x = a() + bc()

Arguments#

usage: %%memray_flamegraph [-h] [--native] [--follow-fork] [--trace-python-allocators] [--leaks | --temporary-allocation-threshold N | --temporary-allocations] [--split-threads]

Named Arguments#

--native

Track native (C/C++) stack frames as well

Default: False

--follow-fork

Record allocations in child processes forked from the tracked script

Default: False

--trace-python-allocators

Record allocations made by the Pymalloc allocator

Default: False

--leaks

Show memory leaks, instead of peak memory usage

Default: False

--temporary-allocation-threshold

Report temporary allocations, as opposed to leaked allocations or high watermark allocations. An allocation is considered temporary if at most N other allocations occur before it is deallocated. With N=0, an allocation is temporary only if it is immediately deallocated before any other allocation occurs.

Default: -1

--temporary-allocations

Equivalent to --temporary-allocation-threshold=1

--split-threads

Do not merge allocations across threads

Default: False