Package: memray
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.NoteMemray 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 theSuccess
Stories discussion page.It really makes a difference!InstallationMemray requires Python 3.7+ and can be easily installed using most common Python
packaging tools. We recommend installing the latest stable release fromPyPIwith pip:python3-mpipinstallmemrayNotice 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 sourceIf you wish to build Memray from source you need the following binary dependencies in your system:libunwind (for Linux)liblz4Check your package manager on how to install these dependencies (for exampleapt-get install libunwind-dev liblz4-devin Debian-based systems
orbrew install lz4in 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 withbrewyou may need to run:exportCFLAGS="-I$(brew--prefixlz4)/include"LDFLAGS="-L$(brew--prefixlz4)/lib -Wl,-rpath,$(brew--prefixlz4)/lib"before installingmemray. 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.exportMACOSX_DEPLOYMENT_TARGET=10.14Once you have the binary dependencies installed, you can clone the repository and follow with the normal building process:gitclonegit@github.com:bloomberg/memray.gitmemraycdmemray
python3-mvenv../memray-env/# just an example, put this wherever you wantsource../memray-env/bin/activate
python3-mpipinstall--upgradepip
python3-mpipinstall-e.-rrequirements-test.txt-rrequirements-extra.txtThis will install Memray in the virtual environment in development mode (the-eof the lastpip installcommand).If you plan to contribute back, you should install the pre-commit hooks:pre-commitinstallThis will ensure that your contribution passes our linting checks.DocumentationYou can find the latest documentation availablehere.UsageThere 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/issuesTo use Memray over a script or a single python file you can usepython3-mmemrayrunmy_script.pyIf you normally run your application withpython3 -m my_module, you can use the-mflag withmemray run:python3-mmemrayrun-mmy_moduleYou can also invoke Memray as a command line tool without having to use-mto invoke it as a module:memrayrunmy_script.py
memrayrun-mmy_moduleThe output will be a binary file (likememray-my_script.2369.bin) that you can analyze in different ways. One way is to use thememray flamegraphcommand to generate a flame graph:memrayflamegraphmy_script.2369.binThis 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 pluginIf you want an easy and convenient way to usememrayin your test suite, you can consider usingpytest-memray. Once installed, this pytest plugin allows you to simply add--memrayto the command line invocation:pytest--memraytests/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.0KiBYou 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")deftest_foobar():# do some stuff that allocates memoryTo learn more on how the plugin can be used and configured check outthe plugin documentation.Native modeMemray supports tracking native C/C++ functions as well as Python functions. This can be especially useful when profiling applications that have C extensions (such asnumpyorpandas) 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--nativeargument when using therunsubcommand:memrayrun--nativemy_script.pyThis 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 modeMemray'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--liveoption to run the script or module in live mode:memrayrun--livemy_script.pyor if you want to execute a module:memrayrun--live-mmy_moduleThis will show the following TUI interface in your terminal:Sorting resultsThe 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 memoryo: Sort by own memorya: Sort by allocation countIn most terminals you can also click the "Sort by Total", "Sort by Own", and "Sort by Allocations" buttons on the footer.The sorted column's heading is underlined.Viewing different threadsBy default, the live command will present the main thread of the program. You can look at different threads of the program by pressing the greater than and less than keys,<and>. In most terminals you can also click the "Previous Thread" and "Next Thread" buttons on the footer.APIIn addition to tracking Python processes from a CLI usingmemray run, it is also possible to programmatically enable tracking within a running Python program.importmemraywithmemray.Tracker("output_file.bin"):print("Allocations will be tracked until the with block ends")For details, see theAPI documentation.LicenseMemray is Apache-2.0 licensed, as found in theLICENSEfile.Code of ConductCode of ConductThis 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 atopensource@bloomberg.net.Security PolicySecurity PolicyIf you believe you have identified a security vulnerability in this project, please send an email to the project team atopensource@bloomberg.net, 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.ContributingWe 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 toopensource@bloomberg.netand we'll get your questions answered as quickly as we can.Contribution LicensingSince this project is distributed under the terms of anopen 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 <random@developer.example.org>The simplest way to accomplish this is to add-sor--signoffto yourgit commitcommand.You must use your real name (sorry, no pseudonyms, and no anonymous contributions).StepsCreate 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.