opensourceprojects.dev

A broadsheet for software that doesn't ask for your email

A memory profiler that traces every Python function call
GitHub RepoImpressions3

Project Description

View on GitHub

A Memory Profiler That Traces Every Python Function Call

You've probably been there: your Python process is eating gigabytes of RAM, and you have no idea which line of code is responsible. You sprinkle some logging around, maybe reach for tracemalloc, and still come up empty. Memray is a memory profiler built by Bloomberg that takes a different approach—it traces every function call so you can see exactly where your memory is going.

What It Does

Memray is a memory profiler for Python that tracks memory allocations across your Python code, native extension modules, and the Python interpreter itself. Unlike sampling profilers that guess at what's happening by taking periodic snapshots, Memray traces every function call, which means it can accurately represent the call stack at the moment each allocation happens.

The tool captures this data and then generates several different types of reports to help you make sense of it—flame graphs being one of the more useful formats. While it's most commonly used as a CLI tool, you can also use it as a library if you need finer-grained control over your profiling tasks.

It's worth noting upfront: Memray only works on Linux and macOS. If you're on Windows, this one isn't for you (at least not yet).

Why It's Cool

There are a few things here that make Memray stand out from the usual profiling suspects.

  • It traces, it doesn't sample. This is the big one. Sampling profilers take snapshots at intervals, which means they can miss short-lived allocations or misattribute them. Because Memray traces every function call, the call stacks it produces are accurate representations of what actually happened. If you've ever chased a memory leak that a sampling profiler couldn't pin down, you'll appreciate why this matters.

  • It handles native code. Python doesn't live in a vacuum—plenty of your memory is being allocated in C and C++ libraries under the hood. Memray captures native calls too, so the entire call stack shows up in your results rather than stopping at the Python/C boundary. That's a meaningful difference when you're debugging something that's allocating in a compiled extension.

  • It's fast enough to actually use. Profiling always adds overhead, but Memray keeps it to a slight slowdown for Python code. Tracking native code is slower, which is why they made it toggleable—you can enable or disable native tracking on demand depending on what you're investigating.

  • It plays nice with threads. Both Python threads and native threads (like C++ threads inside C extensions) are supported. That's not a given in profiling tools, and it matters if you're working with anything concurrent.

  • It solves concrete problems. The README is refreshingly direct about what Memray is for: analyzing allocations to find the cause of high memory usage, finding memory leaks, and locating hotspots in code that cause lots of allocations. No vague promises—just three things you'll likely need at some point.

How to Try It

Memray requires Python 3.9 or newer. The recommended install path is pip:

python3 -m pip install memray

Memray includes a C extension, so releases are distributed as binary wheels as well as source code. If a binary wheel isn't available for your system (Linux x86/x64 or macOS), you'll need to make sure the dependencies are satisfied on the machine where you're installing. There's also a conda-forge package if that's your preferred package manager.

Once installed, you can run it as a CLI tool against your scripts or use it as a library for more targeted profiling. The project's README has the details on the various report types it can generate—flame graphs are the headline feature, but there are others depending on what you're trying to learn.

You can find everything at the repository: github.com/bloomberg/memray.

One more thing worth mentioning: the maintainers actively want to hear from people using it. If you profile something, find a leak, or solve a problem with Memray, they've set up a Success Stories discussion page and would genuinely like to know about it.

Final Thoughts

Memray is a focused tool that does one thing well: it tells you where your memory is going, with enough precision to actually act on. The tracing approach is the right call for memory work (sampling profilers have real limitations here), and the native code support means it won't leave you stranded when the allocation happens outside Python. It's best suited for developers debugging memory issues in production-adjacent code on Linux or macOS—if that's you, it's worth adding to your toolkit. If you've been getting by with guesswork and print statements, this is a better way.


Follow @githubprojects for more developer tools and open source projects.

Back to Projects
Project ID: 4747387f-f33a-4e6b-8e4d-1a45f4b26f30Last updated: October 1, 2026 at 06:01 AM