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.
Note
The flame graph is written to the current working directory, so Jupyter won’t be able to find it if you’ve changed directories.
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]
[--temporal] [--split-threads] [--inverted]
[--max-memory-records MAX_MEMORY_RECORDS]
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
- --temporal
Generate a dynamic flame graph that can analyze allocations in a user-selected time range.
Default:
False- --split-threads
Do not merge allocations across threads
Default:
False- --inverted
Invert the flame graph: use allocators as roots instead of thread entry points
Default:
False- --max-memory-records
Maximum number of memory records to display