• Stars
    star
    12,344
  • Rank 2,485 (Top 0.06 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created about 2 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Memray is a memory profiler for Python


OS Linux OS MacOS PyPI - Python Version PyPI - Implementation PyPI PyPI - Downloads Conda Version Tests Code Style

Memray output

Memray is a memory profiler for Python. It can track memory allocations in Python code, in native extension modules, and in the Python interpreter itself. It can generate several different types of reports to help you analyze the captured memory usage data. While commonly used as a CLI tool, it can also be used as a library to perform more fine-grained profiling tasks.

Notable features:

  • ๐Ÿ•ต๏ธโ€โ™€๏ธ Traces every function call so it can accurately represent the call stack, unlike sampling profilers.
  • โ„ญ Also handles native calls in C/C++ libraries so the entire call stack is present in the results.
  • ๐ŸŽ Blazing fast! Profiling slows the application only slightly. Tracking native code is somewhat slower, but this can be enabled or disabled on demand.
  • ๐Ÿ“ˆ It can generate various reports about the collected memory usage data, like flame graphs.
  • ๐Ÿงต Works with Python threads.
  • ๐Ÿ‘ฝ๐Ÿงต Works with native-threads (e.g. C++ threads in C extensions).

Memray can help with the following problems:

  • Analyze allocations in applications to help discover the cause of high memory usage.
  • Find memory leaks.
  • Find hotspots in code that cause a lot of allocations.

Note
Memray only works on Linux and MacOS, and cannot be installed on other platforms.

Help us improve Memray!

We are constantly looking for feedback from our awesome community โค๏ธ. If you have used Memray to solve a problem, profile an application, find a memory leak or anything else, please let us know! We would love to hear about your experience and how Memray helped you.

Please, consider writing your story in the Success Stories discussion page.

It really makes a difference!

Installation

Memray requires Python 3.7+ and can be easily installed using most common Python packaging tools. We recommend installing the latest stable release from PyPI with pip:

    python3 -m pip install memray

Notice that Memray contains a C extension so releases are distributed as binary wheels as well as the source code. If a binary wheel is not available for your system (Linux x86/x64 or macOS), you'll need to ensure that all the dependencies are satisfied on the system where you are doing the installation.

Building from source

If you wish to build Memray from source you need the following binary dependencies in your system:

  • libunwind (for Linux)
  • liblz4

Check your package manager on how to install these dependencies (for example apt-get install libunwind-dev liblz4-dev in Debian-based systems or brew install lz4 in MacOS). Note that you may need to teach the compiler where to find the header and library files of the dependencies. For example, in MacOS with brew you may need to run:

export CFLAGS="-I$(brew --prefix lz4)/include" LDFLAGS="-L$(brew --prefix lz4)/lib -Wl,-rpath,$(brew --prefix lz4)/lib"

before installing memray. Check the documentation of your package manager to know the location of the header and library files for more detailed information.

If you are building on MacOS, you will also need to set the deployment target.

export MACOSX_DEPLOYMENT_TARGET=10.14

Once you have the binary dependencies installed, you can clone the repository and follow with the normal building process:

git clone [email protected]:bloomberg/memray.git memray
cd memray
python3 -m venv ../memray-env/  # just an example, put this wherever you want
source ../memray-env/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install -e . -r requirements-test.txt -r requirements-extra.txt

This will install Memray in the virtual environment in development mode (the -e of the last pip install command).

If you plan to contribute back, you should install the pre-commit hooks:

pre-commit install

This will ensure that your contribution passes our linting checks.

Documentation

You can find the latest documentation available here.

Usage

There are many ways to use Memray. The easiest way is to use it as a command line tool to run your script, application, or library.

usage: memray [-h] [-v] {run,flamegraph,table,live,tree,parse,summary,stats} ...

Memory profiler for Python applications

Run `memray run` to generate a memory profile report, then use a reporter command
such as `memray flamegraph` or `memray table` to convert the results into HTML.

Example:

    $ python3 -m memray run -o output.bin my_script.py
    $ python3 -m memray flamegraph output.bin

positional arguments:
  {run,flamegraph,table,live,tree,parse,summary,stats}
                        Mode of operation
    run                 Run the specified application and track memory usage
    flamegraph          Generate an HTML flame graph for peak memory usage
    table               Generate an HTML table with all records in the peak memory usage
    live                Remotely monitor allocations in a text-based interface
    tree                Generate a tree view in the terminal for peak memory usage
    parse               Debug a results file by parsing and printing each record in it
    summary             Generate a terminal-based summary report of the functions that allocate most memory
    stats               Generate high level stats of the memory usage in the terminal

optional arguments:
  -h, --help            Show this help message and exit
  -v, --verbose         Increase verbosity. Option is additive and can be specified up to 3 times
  -V, --version         Displays the current version of Memray

Please submit feedback, ideas, and bug reports by filing a new issue at https://github.com/bloomberg/memray/issues

To use Memray over a script or a single python file you can use

python3 -m memray run my_script.py

If you normally run your application with python3 -m my_module, you can use the -m flag with memray run:

python3 -m memray run -m my_module

You can also invoke Memray as a command line tool without having to use -m to invoke it as a module:

memray run my_script.py
memray run -m my_module

The output will be a binary file (like memray-my_script.2369.bin) that you can analyze in different ways. One way is to use the memray flamegraph command to generate a flame graph:

memray flamegraph my_script.2369.bin

This will produce an HTML file with a flame graph of the memory usage that you can inspect with your favorite browser. There are multiple other reporters that you can use to generate other types of reports, some of them generating terminal-based output and some of them generating HTML files. Here is an example of a Memray flamegraph:

Pytest plugin

If you want an easy and convenient way to use memray in your test suite, you can consider using pytest-memray. Once installed, this pytest plugin allows you to simply add --memray to the command line invocation:

pytest --memray tests/

And will automatically get a report like this:

python3 -m pytest tests --memray
=============================================================================================================================== test session starts ================================================================================================================================
platform linux -- Python 3.8.10, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /mypackage, configfile: pytest.ini
plugins: cov-2.12.0, memray-0.1.0
collected 21 items

tests/test_package.py .....................                                                                                                                                                                                                                      [100%]


================================================================================================================================= MEMRAY REPORT ==================================================================================================================================
Allocations results for tests/test_package.py::some_test_that_allocates

	 ๐Ÿ“ฆ Total memory allocated: 24.4MiB
	 ๐Ÿ“ Total allocations: 33929
	 ๐Ÿ“Š Histogram of allocation sizes: |โ–‚   โ–ˆ    |
	 ๐Ÿฅ‡ Biggest allocating functions:
		- parse:/opt/bb/lib/python3.8/ast.py:47 -> 3.0MiB
		- parse:/opt/bb/lib/python3.8/ast.py:47 -> 2.3MiB
		- _visit:/opt/bb/lib/python3.8/site-packages/astroid/transforms.py:62 -> 576.0KiB
		- parse:/opt/bb/lib/python3.8/ast.py:47 -> 517.6KiB
		- __init__:/opt/bb/lib/python3.8/site-packages/astroid/node_classes.py:1353 -> 512.0KiB

You can also use some of the included markers to make tests fail if the execution of said test allocates more memory than allowed:

@pytest.mark.limit_memory("24 MB")
def test_foobar():
    # do some stuff that allocates memory

To learn more on how the plugin can be used and configured check out the plugin documentation.

Native mode

Memray supports tracking native C/C++ functions as well as Python functions. This can be especially useful when profiling applications that have C extensions (such as numpy or pandas) as this gives a holistic vision of how much memory is allocated by the extension and how much is allocated by Python itself.

To activate native tracking, you need to provide the --native argument when using the run subcommand:

memray run --native my_script.py

This will automatically add native information to the result file and it will be automatically used by any reporter (such the flamegraph or table reporters). This means that instead of seeing this in the flamegraphs:

You will now be able to see what's happening inside the Python calls:

Reporters display native frames in a different color than Python frames. They can also be distinguished by looking at the file location in a frame (Python frames will generally be generated from files with a .py extension while native frames will be generated from files with extensions like .c, .cpp or .h).

Live mode

Memray output

Memray's live mode runs a script or a module in a terminal-based interface that allows you to interactively inspect its memory usage while it runs. This is useful for debugging scripts or modules that take a long time to run or that exhibit multiple complex memory patterns. You can use the --live option to run the script or module in live mode:

    memray run --live my_script.py

or if you want to execute a module:

    memray run --live -m my_module

This will show the following TUI interface in your terminal:

Sorting results

The results are displayed in descending order of total memory allocated by a function and the subfunctions called by it. You can change the ordering with the following keyboard shortcuts:

  • t (default): Sort by total memory

  • o: Sort by own memory

  • a: Sort by allocation count

The sorted column is highlighted with < > characters around the title.

Viewing different threads

By default, the live command will present the main thread of the program. You can look at different threads of the program by pressing the left and right arrow keys.

API

In addition to tracking Python processes from a CLI using memray run, it is also possible to programmatically enable tracking within a running Python program.

import memray

with memray.Tracker("output_file.bin"):
    print("Allocations will be tracked until the with block ends")

For details, see the API documentation.

License

Memray is Apache-2.0 licensed, as found in the LICENSE file.

Code of Conduct

This project has adopted a Code of Conduct. If you have any concerns about the Code, or behavior that you have experienced in the project, please contact us at [email protected].

Security Policy

If you believe you have identified a security vulnerability in this project, please send an email to the project team at [email protected], detailing the suspected issue and any methods you've found to reproduce it.

Please do NOT open an issue in the GitHub repository, as we'd prefer to keep vulnerability reports private until we've had an opportunity to review and address them.

Contributing

We welcome your contributions to help us improve and extend this project!

Below you will find some basic steps required to be able to contribute to the project. If you have any questions about this process or any other aspect of contributing to a Bloomberg open source project, feel free to send an email to [email protected] and we'll get your questions answered as quickly as we can.

Contribution Licensing

Since this project is distributed under the terms of an open source license, contributions that you make are licensed under the same terms. In order for us to be able to accept your contributions, we will need explicit confirmation from you that you are able and willing to provide them under these terms, and the mechanism we use to do this is called a Developer's Certificate of Origin (DCO). This is very similar to the process used by the Linux kernel, Samba, and many other major open source projects.

To participate under these terms, all that you must do is include a line like the following as the last line of the commit message for each commit in your contribution:

Signed-Off-By: Random J. Developer <[email protected]>

The simplest way to accomplish this is to add -s or --signoff to your git commit command.

You must use your real name (sorry, no pseudonyms, and no anonymous contributions).

Steps

  • Create an Issue, select 'Feature Request', and explain the proposed change.
  • Follow the guidelines in the issue template presented to you.
  • Submit the Issue.
  • Submit a Pull Request and link it to the Issue by including "#" in the Pull Request summary.

More Repositories

1

blazingmq

A modern high-performance open source message queuing system
C++
2,436
star
2

goldpinger

Debugging tool for Kubernetes which tests and displays connectivity between nodes in the cluster.
JavaScript
2,413
star
3

bde

Basic Development Environment - a set of foundational C++ libraries used at Bloomberg.
C++
1,542
star
4

comdb2

Bloomberg's distributed RDBMS
C
1,283
star
5

pystack

๐Ÿ” ๐Ÿ Like pstack but for Python!
Python
907
star
6

xcdiff

A tool which helps you diff xcodeproj files.
Swift
901
star
7

quantum

Powerful multi-threaded coroutine dispatcher and parallel execution engine
C++
561
star
8

ipydatagrid

Fast Datagrid widget for the Jupyter Notebook and JupyterLab
TypeScript
484
star
9

foml

Foundations of Machine Learning
Handlebars
330
star
10

pytest-memray

pytest plugin for easy integration of memray memory profiler
Python
308
star
11

python-github-webhook

A framework for writing webhooks for GitHub, in Python.
Python
276
star
12

chromium.bb

Chromium source code and modifications
267
star
13

koan

A word2vec negative sampling implementation with correct CBOW update.
C++
261
star
14

blpapi-node

Bloomberg Open API module for node.js
C++
243
star
15

chef-bcpc

Bloomberg Clustered Private Cloud distribution
Python
228
star
16

phabricator-tools

Phabricator Tools
Python
221
star
17

scatteract

Project which implements extraction of data from scatter plots
Jupyter Notebook
207
star
18

record-tuple-polyfill

A polyfill for the ECMAScript Record and Tuple proposal.
JavaScript
159
star
19

pasta-sourcemaps

Pretty (and) Accurate Stack Trace Analysis is an extension to the JavaScript source map format that allows for accurate function name decoding.
TypeScript
159
star
20

collectdwin

CollectdWin - a system statistics collection daemon for Windows, inspired by 'collectd'
C#
123
star
21

clangmetatool

A framework for reusing code in Clang tools
C++
117
star
22

kubernetes-cluster-cookbook

Ruby
100
star
23

quant-research

A collection of projects published by Bloomberg's Quantitative Finance Research team.
Jupyter Notebook
95
star
24

blpapi-http

HTTP wrapper for Bloomberg Open API
TypeScript
83
star
25

amqpprox

An AMQP 0.9.1 proxy server, designed for use in front of an AMQP 0.9.1 compliant message queue broker such as RabbitMQ.
C++
71
star
26

spire-tpm-plugin

Provides agent and server plugins for SPIRE to allow TPM 2-based node attestation.
Go
68
star
27

bde-tools

Tools for developing and building libraries modeled on BDE
Perl
67
star
28

dataless-model-merging

Code release for Dataless Knowledge Fusion by Merging Weights of Language Models (https://openreview.net/forum?id=FCnohuR6AnM)
Python
65
star
29

repofactor

Tools for refactoring history of git repositories
Perl
63
star
30

chef-bach

Chef recipes for Bloomberg's deployment of Hadoop and related components
Ruby
60
star
31

minilmv2.bb

Our open source implementation of MiniLMv2 (https://aclanthology.org/2021.findings-acl.188)
Python
60
star
32

ntf-core

Sockets, timers, resolvers, events, reactors, proactors, and thread pools for asynchronous network programming
C++
60
star
33

wsk

A straightforward and maintainable build system from the Bloomberg Graphics team.
JavaScript
57
star
34

git-adventure-game

An adventure game to help people learn Git
Shell
55
star
35

attrs-strict

Provides runtime validation of attributes specified in Python 'attr'-based data classes.
Python
50
star
36

corokafka

C++ Kafka coroutine library using Quantum dispatcher and wrapping CppKafka
C++
49
star
37

cnn-rnf

Convolutional Neural Networks with Recurrent Neural Filters
Python
49
star
38

ppx_string_interpolation

PPX rewriter that enables string interpolation in OCaml
OCaml
44
star
39

selekt

A Kotlin and familiar Android SQLite database library that uses encryption.
Kotlin
44
star
40

bde_verify

Tool used to format, improve and verify code to BDE guidelines
C++
42
star
41

vault-auth-spire

vault-auth-spire is an authentication plugin for Hashicorp Vault which allows logging into Vault using a Spire provided SVID.
Go
41
star
42

rmqcpp

A batteries included C++ RabbitMQ Client Library/API.
C++
40
star
43

spark-flow

Library for organizing batch processing pipelines in Apache Spark
Scala
40
star
44

startup-python-bootcamp

35
star
45

chef-umami

A tool to automatically generate test code for Chef cookbooks and policies.
Ruby
34
star
46

p1160

P1160 Add Test Polymorphic Memory Resource To Standard Library
C++
33
star
47

pycsvw

A tool to read CSV files with CSVW metadata and transform them into other formats.
Python
32
star
48

bde-allocator-benchmarks

A set of benchmarking tools used to quantify the performance of BDE-style polymorphic allocators.
C++
31
star
49

blpapi-hs

Haskell interface to BLPAPI
Haskell
30
star
50

rwl-bench

A set of benchmark tools for reader/writer locks.
C++
28
star
51

entsum

Open Source / ENTSUM: A Data Set for Entity-Centric Extractive Summarization
Jupyter Notebook
27
star
52

bbit-learning-labs

Learning labs curated by BBIT
Python
26
star
53

consul-cluster-cookbook

Wrapper cookbook which installs and configures a Consul cluster.
Ruby
26
star
54

kbir_keybart

Experimental code used in pre-training the KBIR and KeyBART models
Python
26
star
55

presto-accumulo

Presto Accumulo Integration
Java
25
star
56

sgtb

Structured Gradient Tree Boosting
Python
25
star
57

python-comdb2

Python API to Bloomberg's comdb2 database.
Python
23
star
58

jupyterhub-kdcauthenticator

A Kerberos authenticator module for the JupyterHub platform
Python
22
star
59

docket

Tool to make running test suites easier, using docker-compose.
Go
21
star
60

tzcron

A parser of cron-style scheduling expressions.
Python
20
star
61

blazingmq-sdk-python

Python SDK for BlazingMQ, a modern high-performance open source message queuing system.
Python
20
star
62

constant.js

Immutable/Constant Objects for JavaScript
JavaScript
20
star
63

redis-cookbook

A set of Chef recipes for installing and configuring Redis.
HTML
19
star
64

userchroot

A tool to allow controlled access to 'chroot' functionality by users without root permissions
C
19
star
65

go-testgroup

Helps you organize tests in Go programs into groups.
Go
18
star
66

blazingmq-sdk-java

Java SDK for BlazingMQ, a modern high-performance open source message queuing system.
Java
18
star
67

nginx-cookbook

A set of Chef recipes for installing and configuring Nginx.
Ruby
17
star
68

zookeeper-cookbook

A set of Chef recipes for installing and configuring Apache Zookeeper.
Ruby
17
star
69

mynexttalk

16
star
70

chef-bcs

Bloomberg Cloud Storage Chef application
Ruby
16
star
71

vault-cluster-cookbook

Application cookbook which installs and configures Vault with Consul as a backend.
Ruby
15
star
72

git-adventure-game-builder

A set of tools for building a Git adventure game, to help people learn Git
Shell
15
star
73

emnlp20_depsrl

Research code and scripts used in the paper Semantic Role Labeling as Syntactic Dependency Parsing.
Python
14
star
74

MixCE-acl2023

Implementation of MixCE method described in ACL 2023 paper by Zhang et al.
Python
14
star
75

k8eraid

A relatively simple, unified method for reporting on Kubernetes resource issues.
Go
12
star
76

hackathon-aws-cluster

HTML
11
star
77

coffeechat

A simple web application for arranging 'chats over coffee'.
TypeScript
11
star
78

fast-noise-aware-topic-clustering

Research code and scripts used in the Silburt et al. (2021) EMNLP 2021 paper 'FANATIC: FAst Noise-Aware TopIc Clustering'
Python
10
star
79

emnlp21_fewrel

Code to reproduce the results of the paper 'Towards Realistic Few-Shot Relation Extraction' (EMNLP 2021)
Python
10
star
80

mastering-difficult-conversations

Plan It, Say It, Nail It: Mastering Difficult Conversations
10
star
81

wsk-notify

Simple, customizable console notifications.
JavaScript
10
star
82

jenkins-cluster-cookbook

Ruby
9
star
83

decorator-taxonomy

A taxonomy of Python decorator types.
HTML
9
star
84

pytest-pystack

Pytest plugin that runs PyStack on slow or hanging tests.
Python
9
star
85

tdd-labs

Problems and Solutions for Test-Driven-Development training
JavaScript
9
star
86

argument-relation-transformer-acl2022

This repository contains code for our ACL 2022 Findings paper `Efficient Argument Structure Extraction with Transfer Learning and Active Learning`. We implement an argument structure extraction method based on a pre-trained Transformer model.`
Python
9
star
87

sigir2018-kg-contextualization

8
star
88

bloomberg.github.io

Source code for the https://bloomberg.github.io site
HTML
8
star
89

locking_resource-cookbook

Chef cookbook for serializing access to resources
Ruby
7
star
90

datalake-query-ingester

Python
7
star
91

cobbler-cookbook

A Chef cookbook for installing and maintaining Cobbler
Ruby
7
star
92

p2473

Example code for WG21 paper P2473
Perl
6
star
93

collectd-cookbook

Ruby
6
star
94

Catalyst-Authentication-Credential-GSSAPI

A module that provides integration of the Catalyst web application framework with GSSAPI/SPNEGO HTTP authentication.
Perl
6
star
95

bob-bot

Java
5
star
96

.github

Organization-wide community files
5
star
97

jenkins-procguard

Perl
5
star
98

datalake-query-db-consumer

Python
4
star
99

wsk.example

A sample starter project using wsk.
JavaScript
4
star
100

datalake-metrics-db

Python
3
star